In magicalRecord [on iOS 7 application] there are two NSManagedObjectContexts:
- RootSavingContext of type NSPrivateQueueConcurrencyType
- DefaultContext of type NSMainQueueConcurrencyType
While doing a very heavy database [.sqlite] insert/update/delete how one can make use of above two contexts such that database save [final disk I/O] too happens in background with no use of main thread. Right now the code in MagicalRecord
+ (void)rootContextChanged:(NSNotification *)notification {
if ([NSThread isMainThread] == NO) {
dispatch_async(dispatch_get_main_queue(), ^{
[self rootContextChanged:notification];
});
return;
}
[[self MR_defaultContext] mergeChangesFromContextDidSaveNotification:notification];
}
Is using main thread to save data to database. Instead isn't it possible that the entire thing do takes place in a background with no intervention of main thread at all. Though MagicalRecord exposes methods like MR_Context for creating background NSManagedObjectContext but doing a save operations comes on main thread. I am creating background context in GCD and then uses performBlockAndWait like this:
if ([bContext hasChanges]) {
NSError* __autoreleasing error;
[bContext save:&error];
[bContext.parentContext saveToPersistentStoreAndWait];
}