I am using UIDocument to access core data and trying to setup NSFetchedResultsController in a background thread not to block main UI. It works if i use the uidocument’s managedobjectcontext but not the child. I took some of this code from one of the answers here at stack overflow but are not able to make it work with fetchedresultscontroller.
[ZH peopleDocumentusingBlock:^(UIManagedDocument *peopleDoc){
NSManagedObjectContext *context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
context.parentContext = peopleDoc.managedObjectContext;
[context performBlock:^{
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Person"];
request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"key" ascending:YES]];
[NSFetchedResultsController deleteCacheWithName:nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"imageurl.length>0"];
request.predicate = predicate;
NSError *error = nil;
if(!self.totalNumberOfPeople) //set total number of people for this fetch used for show more records
{
self.totalNumberOfPeople = [context countForFetchRequest:request error:&error];
NSLog(@"total number of people -> %d",self.totalNumberOfPeople);
}
self.fetchedResultsController = [[NSFetchedResultsController alloc]initWithFetchRequest:request managedObjectContext:context sectionNameKeyPath:nil cacheName:@"peopleCache"];
dispatch_async(dispatch_get_main_queue(), ^{
[self.delegate refreshPeoplelTableview];
NSLog(@"fetched objects -> %d",[self.fetchedResultsController.fetchedObjects count]);
NSLog(@"total number of people -> %d",self.totalNumberOfPeople);
});
}];
}];
Actually context countforfetchrequest does but not fetcheresultscontroller.
Thanks in advance!
You are using a “semi-permanant” FRC (it is an instance variable of your controller), but setting it to use a “temporary” MOC, that will go away when no one is using it anymore.
I do not follow what you are really trying to do, but I would think if you store the “temporary” MOC, it will work. Though, you then have to ask why you are doing all this in the first place.
Insert the code between parentContext and performBlock