基于核心数据文档的应用程序与全球持久性商店(Core Data Document

编程入门 行业动态 更新时间:2024-10-07 04:34:19
基于核心数据文档的应用程序与全球持久性商店(Core Data Document-Based Application with Global Persistent Store)

我有一个基于文档的核心数据应用程序,它的工作原理。 我想添加对全球持久性商店的支持来保存一个物品库。

我已阅读了大部分相关文档,并了解我应该在托管对象模型中使用配置。 我定义了两个配置:“DocumentConfiguration”和“LibraryConfiguration”。 文档配置中的实体只在文档配置中,库配置中的实体只在库配置中 - 即不重叠。

然后文档会说“您在创建协调器时使用此模型”。 但是我实际上并没有创建自己的持久性存储协调器,因为我使用的是默认的NSPersistentDocument协调器。

关于如何最好地进行并帮助消除我可能产生的任何误解的几个问题:

答:我会在NSPersistentDocument中获得NSPersistentStoreCoordinator,然后沿着以下方向向其添加新的持久性存储:

NSPersistentStoreCoordinator * coordinator = [[myDocument managedObjectContext] persistentStoreCoordinator]; [coordinator addPersistentStoreWithType:NSXMLStoreType configuration:@"LibraryConfiguration" URL:url options:nil error:&error];

我在考虑这可能是一个问题,因为我没有在NSPersistentDocument的持久存储协调器中提供其他配置定义(“DocumentConfiguration”),因为我使用的是NSPersistentDocument提供的默认值。 我猜它可能会在保存文档时使用nil。 如果是这样,这会是一个问题吗? 也就是说,如果没有为所有持久性存储(在这种情况下为两个)定义相同的配置,协调器如何知道哪个持久性存储可以保存具有给定配置定义的实体? 我能够在创建/保存NSPersistentDocument的持久存储之前设置配置(“DocumentConfiguration”)吗? 从NSPersistentDocument文档:

保存一个新文档会添加一个默认类型的商店和所选的URL,并在上下文中调用save:。 对于现有的文档,保存仅在上下文中调用save:。

B.创建我自己的NSPersistentStoreCoordinator和NSManagedObjectContext实例,添加两个定义了配置的持久性存储,然后让NSPersistentDocument使用这些NSPersistentStoreCoordinator和NSManagedObjectContext实例,并释放旧的实例会更好吗? 如果是这样,我将如何为addPersistentStoreWithType:...方法指定NSPersistentDocument的url? 看起来这个URL只有在未命名的文档被保存后才知道。 (测试这一点,在文档首次保存之前,似乎没有任何临时持久存储(通过持久存储协调器上的方法persistentStores))。

C.或者最好是单独保留NSPersistentDocument,并创建我自己的NSPersistentStoreCoordinator实例,专门用于持久性库存储和托管库对象模型? 文档中说NSPersistentStoreCoordinator的多个实例应该用在多线程的Core Data应用程序中,但我不需要多线程的Core Data支持。 是否需要有两个NSPersistentStoreCoordinator实例 - 一个用于库和一个用于文档(直觉表示这不是必需的,可能不是正确的方法)?

有什么建议么?

I've got a document-based Core Data application which works as is. I would like to add support for a global persistent store to hold a library of items.

I've read most of the relevant docs, and understand that I should use configurations in the managed object models. I've defined two configurations: "DocumentConfiguration" and "LibraryConfiguration". The entities in the document configuration are only in the document configuration, and the entities in the library configuration are only in the library configuration -- i.e., no overlap.

The docs then say "You then use this model when you create a coordinator". But I don't actually create my own persistent store coordinator since I'm using the default NSPersistentDocument coordinator.

A few questions on how best to proceed and help clear up any misunderstandings I might have:

A. Would I obtain the NSPersistentStoreCoordinator in the NSPersistentDocument and then add a new persistent store to it along the lines of:

NSPersistentStoreCoordinator * coordinator = [[myDocument managedObjectContext] persistentStoreCoordinator]; [coordinator addPersistentStoreWithType:NSXMLStoreType configuration:@"LibraryConfiguration" URL:url options:nil error:&error];

I'm thinking that this may be a problem because I haven't provided the other configuration definition ("DocumentConfiguration") in the NSPersistentDocument's persistent store coordinator as I'm using the default provided by NSPersistentDocument. I'm guessing it would probably use nil when the time came to save the document. And if so, would this be a problem? I.e., how would the coordinator know which persistent store to save an entity with a given configuration definition if the same configurations are not defined for all the persistent stores (in this case two)? Am I able to set the configuration (to "DocumentConfiguration") of the NSPersistentDocument's persistent store before it has been created/saved? From the NSPersistentDocument docs:

Saving a new document adds a store of the default type with the chosen URL and invokes save: on the context. For an existing document, a save just invokes save: on the context.

B. Would it be better to create my own NSPersistentStoreCoordinator and NSManagedObjectContext instances, adding the two persistent stores with configurations defined, and then make the NSPersistentDocument use these NSPersistentStoreCoordinator and NSManagedObjectContext instances, and free the old ones? If so, how would I specify the url for the NSPersistentDocument for the addPersistentStoreWithType:... method? It seems this URL is only known once the untitled document has been saved. (Testing this, there does not appear to be any temporary persistent store (via method persistentStores on the persistent store coordinator) until the document is saved for the first time).

C. Or would it be better to leave NSPersistentDocument alone, and create my own NSPersistentStoreCoordinator instance that I use exclusively for the persistent library store and managed library object model? The docs say that multiple instances of NSPersistentStoreCoordinator should be used in multithreaded Core Data applications, but I don't require multithreaded Core Data support. Is it desirable to have two instances of NSPersistentStoreCoordinator -- one for the library and one for documents (intuition says that this is not necessary and probably not the correct approach)?

Any suggestions?

最满意答案

我使用的解决方案很好,基于上面的C)。 我离开了NSPersistentDocument,它只是持久存储协调器,而是创建了我自己专用于持久库存储(全局存储)的NSPersistentStoreCoordinator实例。

我可以将商店的配置设置为自定义值,以防我想稍后有多个商店与此持久存储协调器相关联(例如,“库配置”)。 由于库存储由不同于NSPersistentDocument的持久存储协调器的持久存储协调器管理,因此我不需要关心为NSPersistentDocument的持久存储指定配置。

The solution I used that works well is based on C) above. I leave the NSPersistentDocument and it's persistent store coordinator alone, and instead create my own NSPersistentStoreCoordinator instance that I use exclusively for the persistent library store (global store).

I can set the configuration for the store to a custom value in case I want to have multiple stores associated with this persistent store coordinator later (e.g., "LibraryConfiguration"). Since the library store is managed by a persistent store coordinator different from the NSPersistentDocument's persistent store coordinator, I don't need to be concerned about specifying a configuration for the NSPersistentDocument's persistent stores.

更多推荐

本文发布于:2023-08-07 11:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464060.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:持久性   应用程序   商店   核心   文档

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!