Reputation: 941
I'm attempting a fetch request with the following code:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"dateModified > CAST(CAST(now(), \"NSNumber\") - %d, \"NSDate\")", (30*86400)];
NSFetchRequest *itemRequest = [NSFetchRequest fetchRequestWithEntityName:@"Item"];
[itemRequest setPredicate:predicate];
NSArray *items = [[self managedObjectContext] executeFetchRequest:itemRequest error:nil];
...in order to fetch items where the 'dateModified > |now() - 30 days|'. The problem is that the above predicate works correctly (fetches properly) until I save my NSPersistentDocument as SQLite and re-open the document. When I re-open the document I get the following error:
Unsupported function expression CAST(CAST(now(), "NSNumber") - 2592000, "NSDate")
Why does this predicate work correctly up until save or if I save the document as XML, but does not work correctly if I save as SQLite? Is there a way to use this predicate with the SQlite store type?
P.S. I have also tried forming the predicate with the following code but get the same error:
//"now()" is a function and takes no arguments
NSExpression * now = [NSExpression expressionForFunction:@"now" arguments:[NSArray array]];
//CAST(now(), 'NSNumber')
NSArray * castNowArguments = [NSArray arrayWithObjects:now, [NSExpression expressionForConstantValue:@"NSNumber"], nil];
NSExpression * castNow = [NSExpression expressionForFunction:@"castObject:toType:" arguments:castNowArguments];
//CAST(now(), 'NSNumber') [+/-] {a time interval}
NSArray * relativeTimestampArguments = [NSArray arrayWithObjects:castNow, [NSExpression expressionForConstantValue:[NSNumber numberWithDouble:(30*86400)]], nil];
NSExpression * relativeTimestamp = [NSExpression expressionForFunction:@"from:subtract:" arguments:relativeTimestampArguments];
//CAST(CAST(now(), 'NSNumber') [+/-] {a time interval}, 'NSDate')
NSArray * castToDateArguments = [NSArray arrayWithObjects:relativeTimestamp, [NSExpression expressionForConstantValue:@"NSDate"], nil];
NSExpression * castToDate = [NSExpression expressionForFunction:@"castObject:toType:" arguments:castToDateArguments];
NSPredicate *predicate = [NSComparisonPredicate predicateWithLeftExpression:[NSExpression expressionForKeyPath:@"dateModified"]
rightExpression:castToDate
modifier:NSDirectPredicateModifier
type:NSGreaterThanOrEqualToComparison
options:0];
Upvotes: 2
Views: 1528
Reputation: 39
Following code worked out just well in my case:
now() - 2592000
Upvotes: 1
Reputation: 941
I found my own solution...
Function calls are unsupported when fetching data from a SQLite store type. It is only possible to use function calls in fetches for in-memory data. In order to still use this predicate I had to first fetch the data set and then filter the array using:
[allItems filteredArrayUsingPredicate:predicate];
Hope this helps someone!
Upvotes: 2