For my app, I'm fetching quite a lot objects from the core data store, this causes the app to freeze and blocks all UI input. I would want to do the fetching in the background while the app remains responsive and update the tableview
only when the data is available.
For this purpose
I've setup a new NSManagedObjectContext
with NSPrivateQueueConcurrencyType
and made it as a child of the main MOC. While my setup is returning the desired objects it seems like all the processing is still freezing the UI and like there is almost no difference in responsiveness with the old code where everything was happening on the main queue.
According to this article the child contexts setup does not help on keeping the UI responsive while everywhere else on the net I read this is the way to go if you want to relieve the main queue from heavy processing? Am I missing something ?
NSManagedObjectContext *mainMOC = self.mainObjectContext;
NSManagedObjectContext *backgroundMOC = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[backgroundMOC setParentContext:mainMOC];
[backgroundMOC performBlock:^{
//query for objects
NSArray *results = [Product MR_findAllInContext:backgroundMOC];
NSError *childError = nil;
[backgroundMOC save:&childError];
if ( [results count] > 0 ) {
//get objectIDs
NSMutableArray *objectIDs = [NSMutableArray array]
for (NSManagedObject *object in results) {
[objectIDs addObject:[object objectID]];
}
[mainMOC performBlock:^{
//refetch objects on the mainQueue
NSMutableArray *persons = [NSMutableArray array]
for (NSManagedObjectID *objectID in objectIDs) {
[persons addObject:(Person*)[mainMOC objectWithID:objectID]];
}
//return result
if (self.callBack)
self.callBack(persons);
}];
}
}];