将对象从多个存储保存到单个持久存储 [英] Save objects from multiple stores to single persistent store

查看:86
本文介绍了将对象从多个存储保存到单个持久存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个持久存储,对象使用相同的模型。我想在一个上下文中打开这两个商店,但将上下文保存到一个商店,然后安全地删除其中一个商店。我实质上试图将两个持久存储的内容合并到一个持久存储。因为实体有关系,我发现这很困难。



此回答


如果您确实需要在两个商店的对象之间建立关系,或者你真的只想有一个存储,你最好的打算是创建第二个NSPersistentStoreCoordinator和第三个,不同的持久存储来保存合并的对象图。您将需要编写代码以在连接到此第二个NSPersistentStoreCoordinator的受管对象上下文中创建对象的副本。您需要在原始对象具有的副本之间设置相同的关系,但是如何执行此操作取决于您的数据模型。


这使得它看起来很清楚,除了详细的,如何​​,字面上,为新的商店的对象的副本。这是一个迁移问题吗?

解决方案

最终,我使用了以下方法:

  [migrator migrateStoreFromURL:[NSURL fileURLWithPath:incomingPath] 
type:nil
选项:nil
withMappingModel:managedObjectModel
toDestinationURL: [NSURL fileURLWithPath:finalPath]
destinationType:nil
destinationOptions:nil
error:& err];

[persistentStoreCoordinator removePersistentStore:[[persistentStoreCoordinator persistentStores] lastObject] error:& err];

incomingPath中的文件是我导入的商店,文件在finalPath现存的商店,我想合并成。两个商店在同一持久性协调器中打开,并且都使用相同的对象模型。我然后删除了进来的商店,从来没有再看过它;我想我可以在文件系统级别删除它。



对于我的特定数据需要,然后获取所有的记录,删除重复,并保存上下文。



我发布这个答案,因为作为核心数据新手,这个简单的迁移解决了不能保存与不同商店中的对象的关系问题非直观。 p>

I have two persistent stores with objects that use the same model. I would like to open both stores in one context, but save the context to only a single store, and then safely delete one of the stores. I am in essence trying to merge the contents of two persistent stores into a single persistent store. Because the entities have relationships, I am finding this difficult.

From this answer:

If you do need to have relationships between the objects in both stores, or you really just want to have a single store, your best bet would be to create a second NSPersistentStoreCoordinator and a third, distinct persistent store to hold the merged object graph. You will need to write code to create copies of the objects in a managed object context attached to this second NSPersistentStoreCoordinator. You'll need to set up the same relationships among the copies that the original objects had, too, but how you go about doing this depends on your data model.

That makes it seem clear, except for the details of how, literally, to make copies of the objects for the new store. Is this a migration issue?

解决方案

Ultimately, I used the following approach:

[migrator migrateStoreFromURL:[NSURL fileURLWithPath:incomingPath]
                         type:nil 
                      options:nil 
             withMappingModel:managedObjectModel 
             toDestinationURL:[NSURL fileURLWithPath:finalPath] 
              destinationType:nil 
           destinationOptions:nil 
                        error:&err];

[persistentStoreCoordinator removePersistentStore:[[persistentStoreCoordinator persistentStores]lastObject] error:&err];

The file at "incomingPath" was the store I imported, the file at "finalPath" the already-existent store that I wanted to merge into. Both stores were open in the same persistent coordinator, and both use the same object model. I then removed the incoming store and never looked at it again; I suppose I could have deleted it at the file system level.

For my particular data needs, I then fetched all the records, culled out the duplicates, and saved the context.

I publish this answer because as a core data newbie this simple migration solved the "can't save relationships to objects in different stores" problem non-intuitively.

这篇关于将对象从多个存储保存到单个持久存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆