在magicalRecord [在iOS 7应用]有两种NSManagedObjectContexts:MagicalRecord ==总背景?
- 类型的RootSavingContext NSPrivateQueueConcurrencyType
- 类型的DefaultContext NSMainQueueConcurrencyType
虽然做一个非常沉重的数据库[.sqlite]插入/更新/删除 如何使用上述两种上下文,使得 数据库保存[最终磁盘I/O]也在后台 中发生,并且不使用主线程。 眼下在MagicalRecord
+ (void)rootContextChanged:(NSNotification *)notification {
if ([NSThread isMainThread] == NO) {
dispatch_async(dispatch_get_main_queue(), ^{
[self rootContextChanged:notification];
});
return;
}
[[self MR_defaultContext] mergeChangesFromContextDidSaveNotification:notification];
}
的代码使用主线程将数据保存到数据库中。 相反,整个事情不可能发生在没有主线程干预的背景下。虽然MagicalRecord公开了像MR_Context这样的方法来创建背景NSManagedObjectContext,但是在主线程中执行了保存操作。我创造了GCD的背景环境,然后使用performBlockAndWait这样的:
if ([bContext hasChanges]) {
NSError* __autoreleasing error;
[bContext save:&error];
[bContext.parentContext saveToPersistentStoreAndWait];
}