Cocoa (Mac):将基于核心数据的应用程序从/合并到多个保存文件中
我正在 OS X Lion 中开发一个基于 Core Data 的 Cocoa 应用程序,我需要能够将数据模型的不同部分(比如 A 部分和 B 部分)保存到单独的文件中。
我需要能够将两个文件一起保存为项目文件/包,但还需要能够独立于 PartB 加载和保存 PartA。
加载新的partA 文件应替换当前与partA 关联的所有数据。 保存 A 部分不应保存 B 部分中更改的数据。
A 部分中的实体确实需要维护与 B 部分中的实体的关系,但这些关系可能(而且很可能必须)很弱。
我的主要问题是:实现所需功能的最佳方法是什么?
我的第一种方法是一个包含两个配置的 NSManagedObjectModel,每个部分一个。我有两个分配给我的 NSPersistentStoreCoordinator 实例,每个实例对应一个配置。以及一个分配有 storeCoordinator
的 NSManagedObjectContext
实例。
保存和打开单独的文件是目前我在此方法中主要关注的问题。 NSManagedObjectContext
-save:
消息似乎保存了这两种配置。是否可以仅保存对属于 NSManagedObjectModel
特定配置的对象所做的更改?或者我需要两个 NSManagedObjectContext 实例,每个配置一个?
通过将存储添加到partA的persistentStoreCoordinator
来打开文件会将数据添加到上下文中,但到目前为止我还无法替换数据。有没有办法知道哪个存储与某个配置相关联,也许可以通过向persistentStoreCoordinator发送消息来实现?
我还在考虑第二种方法:为数据模型中的每个配置设置两个 NSPersistentDocument 子类,以便能够将数据保存到单独的文件中。但我不确定拥有两个单独的 NSManagedObjectContext 实例是否允许在不同配置中设置 NSManagedObject 子类之间的关系?
如果有人有一个好主意或可以为我指出正确的方向,甚至有一个如何实现上述功能的示例,我将不胜感激。
I'm working in OS X Lion on a Core Data based Cocoa application where I need to be able to save different parts, lets say partA and partB, of the data model into separate files.
I need to be able to save both files together as a project file/package, but also need to be able to load and save partA independently from partB.
Loading a new partA file should replace all data currently associated with partA.
Saving partA should not save data changed in partB.
Entities in partA do need to maintain relationships with entities in partB but these can (and most likely have to) be weak.
My main question is: What would be the best approach for implementing the desired features?
My first approach has been one NSManagedObjectModel
containing two configurations, one for each of the parts. I have two NSPersistentStore
instances assigned to my NSPersistentStoreCoordinator
one for each of the configurations. And one NSManagedObjectContext
instance with the storeCoordinator
assigned to it.
Saving and opening separate files are currently my main concerns in this appraoch. NSManagedObjectContext
-save:
message seems to save both configurations. Is it possible to only save changes made to objects that belong to a specific configuration of the NSManagedObjectModel
? Or would I need two NSManagedObjectContext
instances, one for each of the configurations?
Opening a file by adding a store to the persistentStoreCoordinator
for partA adds data to the context and so far I have not been able to replace data. Is there a way to know which store is associated with a certain configuration, perhaps by sending a message to the persistentStoreCoordinator
?
I'm also thinking of a second approach: Setting up two subclasses of NSPersistentDocument
one for each configuration in my data model to be able to save the data into separate files. But I'm not sure if having two separate NSManagedObjectContext
instances would allow setting up relationships between NSManagedObject
subclasses in the different configurations?
If anyone has a good idea or can point me in the right direction or even has an example of how I could implement the above features, that would be highly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论