I encountered the same problem when working with a child of NSManagedObjectContext. I create it as follows
NSManagedObjectContext *workerMOC = nil; workerMOC = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType]; workerMOC.parentContext = self.moc;
Now if I do this:
[workerMOC performBlock:^{ NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Company"]; [fetchRequest setReturnsObjectsAsFaults:NO]; NSArray *allCompanies = [workerMOC executeFetchRequest:fetchRequest error:nil]; }];
I get errors in all companies. Which, of course, just for clarification, does not happen if I execute a fetch request on self.moc.
However, I get relevant preliminary results if I use the following approach:
[workerMOC performBlock:^{ NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease]; fetchRequest.entity = [NSEntityDescription entityForName:@"Company" inManagedObjectContext:workerMOC]; [fetchRequest setReturnsObjectsAsFaults:NO]; NSArray *allCompanies = [workerMOC.persistentStoreCoordinator executeRequest:fetchRequest withContext:workerMOC error:nil]; }];
So, it seems that fetching at NSManagedObjectContexts, directly related to NSPsistentStoreCoordinator, works just fine. But in the case of child NSManagedObjectContexts that are not directly bound to the repository, but rather to the parent context, do not behave as expected. I could not find anything related to this issue in Apple docs, but still, I don't think this is a mistake.
Bartek chlebek
source share