I am using CoreData and executing the following command returns a number of objects:
[Contact findAll];
However when I setup a simple NSFetchrequest like so, I get 0 objects.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
fetchRequest.entity = [NSEntityDescription entityForName:@"Contact" inManagedObjectContext:self.context];
fetchRequest.sortDescriptors = [NSArray arrayWithObject:[[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES]];
self.fetchedResultsController = [[NSFetchedResultsController alloc]
initWithFetchRequest:fetchRequest
managedObjectContext:self.context
sectionNameKeyPath:nil
cacheName:nil];
NSError *error;
NSArray *array = [self.context executeFetchRequest:fetchRequest error:&error];
Please tell me what I am doing wrong here. Since I don’t have a Predicate set, I would expect to get the same number of objects, but I get an array with 0 objects and error = nil.
Please note that self.context is not nil.
If you’re using RESTKit, I’m guessing that RESTKit is setting up its own Core Data context/store and when you’re doing it manually, you’re using the default ones provided by Apple’s Xcode template. Stop using
self.contextand get the context from RESTKit, or use one of RESTKit’s convenience methods such asobjectsWithFetchRequest:.