简体   繁体   English

多个NSPersistentStoreCoordinator实例可以连接到同一个底层SQLite持久性存储吗?

[英]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堆栈( NSManagedObjectContextNSManagedPersistentStoreCoordinatorNSManagedObjectModel )吗? 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.

相关问题 在 NSPersistentStoreCoordinator 上调用 destroyPersistentStore 后,我应该删除底层持久存储文件吗? - Should I delete underlying persistent store files after calling destroyPersistentStore on a NSPersistentStoreCoordinator? NSPersistentStoreCoordinator没有持久性存储(无法打开) - NSPersistentStoreCoordinator has no persistent stores (can't open) SQLite持久存储作为缓存 - SQLite Persistent Store as a cache 两个NSPersistentStoreCoordinator可以使用相同的DB吗? - Can two NSPersistentStoreCoordinator use the same DB? NSPersistentStoreCoordinator有两种类型的持久存储? - NSPersistentStoreCoordinator with two types of persistent stores? 多个NSManagedObjectContext是否可以共享同一个NSManagedObjectModel? 和相同的NSPersistentStoreCoordinator实例 - is it possible for multiple NSManagedObjectContext share same NSManagedObjectModel? and same NSPersistentStoreCoordinator instance 如何使用 Swift 在 Firebase 中存储具有相同关键字的不同数据的多个实例? - How can I store multiple instances of different data with the same keyword in Firebase using Swift? 为现有 sqlite 上的持久存储管理器更改 NSPersistentStoreFileProtectionKey - Changing NSPersistentStoreFileProtectionKey for persistent store manager on existing sqlite CoreData多个持久性存储协调器? - CoreData multiple persistent store coordinator? iOS中的多个持久性存储协调器 - Multiple persistent store coordinator in iOS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM