问题描述
我已经阅读过的关于在多线程上使用Core Data的所有内容都谈到使用多个 NSManagedObjectContext
实例共享一个 NSPersistentStoreCoordinator
。这是理解,我已经使它在一个应用程序使用Core数据在主线程上支持UI,并有一个后台获取操作,可能需要一段时间才能运行。
Everything I've read about using Core Data on multiple threads talks about using multiple NSManagedObjectContext
instances sharing a single NSPersistentStoreCoordinator
. 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.
问题是对底层SQLite持久化存储的访问是由 NSPersistentStoreCoordinator
序列化的,所以仍然有一些场合,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.
后台获取操作不会更新数据,只能从中读取数据。我可以设置一个完全并行的核心数据栈( NSManagedObjectContext
, NSManagedPersistentStoreCoordinator
和 NSManagedObjectModel
)在后台线程连接到相同的底层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? It seems like this would give complete concurrency between the UI thread and the background fetch operation.
推荐答案
我自己暂时的答案现在是是。
My own tentative answer to this is now yes.
我通过传递 NSPersistentStore
实例来初始化我的后台操作。在后台线程中,此商店的属性(包括网址)用于创建如下所示的全新Core数据堆栈:
I initialize my background operation by passing it the NSPersistentStore
instance. On the background thread, the properties of this store, including the URL, are used to create a whole new Core Data stack like this:
// 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];
然后我使用这个新创建的 NSManagedObjectContext
instance。
I then perform the background fetch using this newly created NSManagedObjectContext
instance.
一切似乎都很好。我还没有接受我自己的答案,因为我很想有人为我的发现提供支持或矛盾的证据。
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.
这篇关于多个NSPersistentStoreCoordinator实例可以连接到同一个底层SQLite持久存储吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!