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:@"as开发者_Python百科dsad"];
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?
Because you need to do one more thing: performFetch.
Here's the details in the docs:
performFetch: Executes the receiver’s fetch request.
- (BOOL)performFetch:(NSError **)error Parameters error If the fetch is not successful, upon return contains an error object that describes the problem. Return Value YES if the fetch executed successfully, otherwise NO.
Discussion After executing this method, you can access the receiver’s the fetched objects with the property fetchedObjects.
精彩评论