1
votes

First of all, this is my first question, so be nice:).

Have any of You noticed weird behaviour math functions in NSPredicate?

My example is:

1) I have departure class with interface:

@interface OTDeparture : NSManagedObject
@property (nonatomic, retain) NSNumber * position;
@end

2) i create fetch request with predicate:

NSPredicate *predicate=[NSPredicate predicateWithFormat:@"(position-1)*(position-1) > 0"];

and the result is always zero objects (predicate is always NO). Also format "(2-1)*(2-1) > 0" and "(position-1)*position > 0" does the same thing.

These predicate formats work as expected (return object as expected):

"2*(position-1) > 0"
"2*2 > 0"
"position-3 > 0"

Am I doing something wrong, or is this really a bug? I'm not using any third party stuff, just code provided by Apple.

1

1 Answers

2
votes

Seems like you're right, there are some in CoreData :(( I have the same issue - Is there any way to use NSPredicate to filter objects using distance calculation between points in CoreData seems like the only way to receive what you want is to use predicateWithBlock: method, though I haven't tried it myself yet. And note that predicateWithBlock: works only for in-memory stores. So most probably you will end up filtering the data by just going through all entities, of course you can try to make at least some filtering before doing this.