Reputation: 29925
req = [[NSFetchRequest alloc] init];
// entity
ent = [NSEntityDescription entityForName:@"Medicine" inManagedObjectContext:context];
[req setEntity:ent];
// predicate
pred = [NSPredicate predicateWithFormat:@"date > %@",referenceDate];
[req setPredicate:pred];
// sort descriptor
sorter = [NSSortDescriptor sortDescriptorWithKey:@"date" ascending:YES];
[req setSortDescriptors:[NSArray arrayWithObjects:sorter, nil]];
NSFetchedResultsController *frc = [[NSFetchedResultsController alloc] initWithFetchRequest:req managedObjectContext:context sectionNameKeyPath:@"date" cacheName:@"asdsad"];
NSLog(@"%@",[frc fetchedObjects]); // returns (null)
//NSArray *frc = [context executeFetchRequest:req error:nil];
//NSLog(@"%@",frc); // returns 4 objects
As you can see by my code I've got two different bits at the end.
The first code (uncommented) returns null in the NSLog.
The second code (commented) returns an array of 4 objects from the context.
Any reason why this is happening? Am I doing something wrong?
Upvotes: 2
Views: 1701
Reputation: 26652
Because you need to do one more thing: performFetch.
Here's the details in the docs:
performFetch: Executes the receiver’s fetch request.
Discussion After executing this method, you can access the receiver’s the fetched objects with the property fetchedObjects.
Upvotes: 6