[英]Can multiple NSPersistentStoreCoordinator instances be connected to the same underlying SQLite persistent store?
Everything I've read about using Core Data on multiple threads talks about using multiple NSManagedObjectContext
instances sharing a single NSPersistentStoreCoordinator
. 我读过的关于在多个线程上使用Core Data的所有内容都讨论了使用多个共享单个
NSPersistentStoreCoordinator
NSManagedObjectContext
实例。 This is understood and I've made it work in an app that uses Core Data on the main thread in support of the UI and has a background fetch operation that can take a while to run. 这是理解的,我已经使它在一个应用程序中工作,该应用程序在主线程上使用Core Data来支持UI,并且具有可能需要一段时间才能运行的后台获取操作。
The problem is that access to the underlying SQLite persistent store is serialized by the NSPersistentStoreCoordinator
, so there are still occasions where the UI is blocked by the background fetch operation. 问题是
NSPersistentStoreCoordinator
基础SQLite持久性存储的访问进行序列化,因此仍然存在背景提取操作阻止UI的情况。
The background fetch operation will never update the data, only read from it. 后台获取操作永远不会更新数据,只能从中读取数据。 Can I set up an entirely parallel Core Data stack (
NSManagedObjectContext
, NSManagedPersistentStoreCoordinator
, and NSManagedObjectModel
) on the background thread connected to the same underlying SQLite persistent store? 我可以在连接到相同底层SQLite持久性存储的后台线程上设置完全并行的Core Data堆栈(
NSManagedObjectContext
, NSManagedPersistentStoreCoordinator
和NSManagedObjectModel
)吗? It seems like this would give complete concurrency between the UI thread and the background fetch operation. 看起来这样可以在UI线程和后台获取操作之间提供完全的并发性。
My own tentative answer to this is now yes . 我对此的初步答案现在是肯定的 。
I initialize my background operation by passing it the NSPersistentStore
instance. 我通过传递
NSPersistentStore
实例初始化我的后台操作。 On the background thread, the properties of this store, including the URL, are used to create a whole new Core Data stack like this: 在后台线程中,此存储的属性(包括URL)用于创建一个全新的Core Data堆栈,如下所示:
// create managed object model
NSURL *modelUrl = [[NSBundle bundleForClass:[self class]] URLForResource:@"..." withExtension:@"..."];
NSManagedObjectModel *managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelUrl];
// create persistent store coordinator
NSPersistentStoreCoordinator *persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedObjectModel];
NSError *error = nil;
[persistentStoreCoordinator addPersistentStoreWithType:[store type]
configuration:[store configurationName]
URL:[store URL]
options:[store options]
error:&error];
// create managed object context
NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init];
[context setPersistentStoreCoordinator:persistentStoreCoordinator];
[persistentStoreCoordinator release];
[managedObjectModel release];
I then perform the background fetch using this newly created NSManagedObjectContext
instance. 然后,我使用这个新创建的
NSManagedObjectContext
实例执行后台获取。
Everything seems to work just fine. 一切似乎都很好。 I'm not yet accepting my own answer, though, as I would love to have someone provide supporting or contradicting evidence to my findings.
不过,我还没有接受我自己的答案,因为我希望有人能为我的研究结果提供支持或反对的证据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.