关于核心数据NSManagedObject深拷贝的疑问
我遇到一种情况,我必须将一个 NSManagedObject 从主上下文复制到编辑上下文中。对于大多数人来说,这听起来没有必要,因为我在 Stackoverflow 中看到过类似的情况,但我看起来需要它。
在我的应用程序中,选项卡栏中有许多视图,每个视图处理与其他视图相关的不同信息。我认为我需要多个 MOC,因为用户可能从一个选项卡跳转到另一个选项卡,并在某些选项卡中留下未保存的更改,但它可能会在其他选项卡/视图中保存数据,因此如果发生这种情况,其余视图中的更改将在未经用户同意的情况下保存在最坏的情况下会使应用程序崩溃。
为了添加新信息,我通过使用添加 MOC,然后合并两个 MOC 中的更改来完成,但编辑并不那么容易。我在这里看到了类似的情况 在 Stackoverflow 中,但应用程序崩溃了,因为我的数据模型似乎没有使用 NSMutableSet 来建立关系(我不认为我有多对多关系,只是一对多)我认为它可以修改,这样我就可以像属性一样检索关系,
for (NSString *attr in relationships) {
[cloned setValue:[source valueForKey:attr] forKey:attr];
}
但我不知道如何合并克隆对象和原始对象的更改。我想我可以从主上下文中删除该对象,然后合并两个上下文并在主上下文中保存更改,但我不知道这是否是正确的方法。我还担心数据库完整性,因为我不确定反向关系是否会保留对克隆对象的相同引用,就好像它是原始对象一样。
有人可以告诉我这一点吗?
I have a situation where I must copy one NSManagedObject from the main context into an editing context. It sounds unnecessary to most people as I have seen in similar situations described in Stackoverflow but I looks like I need it.
In my app there are many views in a tab bar and every view handles different information that is related to the other views. I think I need multiple MOCs since the user may jump from tab to tab and leave unsaved changes in some tab but maybe it saves data in some other tab/view so if that happens the changes in the rest of the views are saved without user consent and in the worst case scenario makes the app crash.
For adding new information I got away by using an adding MOC and then merging changes in both MOCs but for editing is not that easy. I saw a similar situation here in Stackoverflow but the app crashes since my data model doesn't seem to use NSMutableSet for the relationships (I don't think I have a many-to-many relationship, just one-to-many) I think it can be modified so I can retrieve the relationships as if they were attributes
for (NSString *attr in relationships) {
[cloned setValue:[source valueForKey:attr] forKey:attr];
}
but I don't know how to merge the changes of the cloned and original objects. I think I could just delete the object from the main context, then merge both contexts and save changes in the main context but I don't know if is the right way to do it. I'm also concerned about database integrity since I'm not sure that the inverse relationships will keep the same reference to the cloned object as if it were the original one.
Can some one please enlighten me about this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
哇,这里面有很多问题和随机性。首先,您不需要为自己的问题添加评论,最好编辑问题本身。
其次,如果您运行单线程应用程序,则不需要多个 NSManagedObjectContext 实例。您的整个应用程序可以轻松地在单个实例上运行。多个上下文适用于罕见的边缘情况和多线程情况。
通过使用单个上下文,它将解决您尝试克隆时的所有问题。但是,如果您仍然想知道如何进行
NSManagedObject
的深层复制,您可以从我的书中的示例代码中获取一些指导,网址为 务实的程序员;其源代码可以免费下载。更新
所有
NSManagedObjectContext
实例都是“编辑实例”。每个线程只需要一个上下文。如果您想在保存之前删除未插入的实体,您可以轻松地询问它们的上下文,并且可以在实体发生更改时轻松保存它们。除了一些极端边缘情况,您不需要第二个上下文。更新
您仍在为自己创造比您需要的更多的工作。您无需保存对象即可让视图消失。您可以在上下文中将任意数量的对象保留为未保存状态。
您将花费更多的时间来调试处理对象的深层副本,而不是您所描述的设计所值得的时间。如果您要使用多个上下文,请确保它们附加到同一个
NSPercientStoreCoordinator
,这样您就不需要复制对象,您只需保存“辅助”上下文即可然后在主上下文中捕获保存通知。Wow, lots of questions and randomness in this one. First, you do not need to add comments to your own question, better to edit the question itself.
Second, you don't need multiple
NSManagedObjectContext
instances if you are running a single threaded application. Your entire application can easily run off of a single instance. Multiple contexts are for rare edge cases and multi-threading situations.By using a single context it will resolve all of your issues with your attempts to clone. However, if you are still wondering how to do a deep copy of a
NSManagedObject
you can get some guidance from the example code in my book at The Pragmatic Programmers; the source code of which is free to download.Update
All
NSManagedObjectContext
instances are "editing ones". You only need a single context per thread. You can easily ask the context for the non-inserted entities if you want to delete them before a save and you can easily save entities as they are changed. Except for some extreme edge cases you do not need a second context.Update
You are still creating more work for yourself then you need to. You don't need to save an object for the view to go away. You can leave as many objects as you want in an unsaved state in the context.
You are going to spend more time debugging dealing a deep copy of objects than it is worth with the design you are describing. If you are going to use more than one context, make sure they are attached to the same
NSPersistentStoreCoordinator
so that you don't need to copy the objects around, you can just save the "secondary" context and then catch the save notification in the main context.