Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS NSDate Core Data Compare fetch request not working

maybe you can help me. What is wrong with this code:

-(NSMutableArray *)returnItemsWithName:(NSString *)name{

    NSFetchRequest *fetch=[[NSFetchRequest alloc] init];
    NSEntityDescription *entity=[NSEntityDescription entityForName:@"XYZ" inManagedObjectContext:[self managedObjectContext]];
    [fetch setEntity:entity];
    NSDate *sevenDaysAgo = [appDelegate dateByAddingDays:-7 toDate:[NSDate date]];
NSPredicate *pred= [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"originTime >= %@", sevenDaysAgo]];
    [fetch setPredicate:pred];
    NSError *fetchError=nil;
    NSMutableArray *fetchedObjs = [[[self managedObjectContext] executeFetchRequest:fetch error:&fetchError] retain];
    if (fetchError!=nil) {
        return nil;
    }

    return fetchedObjs;

}

the line

fetchedObjs = [[[self managedObjectContext] executeFetchRequest:fetch error:&fetchError] retain]; 

crashes with the error:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "originTime >= 2011-02-28 21:07:37 +0000"'

All the objects are NOT nil and also originDate is a NSDate in the CD database

like image 320
creeps Avatar asked Mar 07 '11 21:03

creeps


1 Answers

Your problem is this:

[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"originTime >= %@", sevenDaysAgo]];

predicateWithFormat: already wants a format string. It is unnecessary and, as you've found, wrong to do what you're doing. It's pretty easy to fix though:

[NSPredicate predicateWithFormat:@"originTime >= %@", sevenDaysAgo];

That will work just fine.

like image 152
Dave DeLong Avatar answered Nov 15 '22 07:11

Dave DeLong