核心数据:以编程方式更改删除规则 [英] Core Data: change delete rule programmatically

查看:157
本文介绍了核心数据:以编程方式更改删除规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的iOS应用程式会从第三方资料库下载纪录,并使用CoreData储存在本机中。用户能够编辑所存储的记录的某些方面,例如,除了来自数据库的注释之外还可以添加用户注释。

My iOS app downloads record from a 3rd party database, and stores them locally using CoreData. The user is able to edit some aspects of the stored records, eg user notes can be added in addition to notes from the database.

偶尔,数据库被更新,我提供刷新功能。

Occasionally, the database gets updated, and I provide a refresh function. Instead of checking what parts of the entries are different, I just brute-force remove the old one and replace it with a new one.

但是,这也会删除用户的信息,而不是检查条目的哪些部分不同,我只是强制删除旧的和替换一个新的。笔记。我尝试在刷新之前保存它们,并在插入新条目后重新添加它们,但是一旦原始条目被删除,用户注释也被删除,因为级联删除规则。如果我将注释的删除规则设置为无操作,则所有注释不会被删除。

However, this also removes the user notes. I tried saving them before the refresh, and re-adding them after inserting the new entry, but once the original entry gets deleted, the user note is also deleted because of the "Cascade" delete rule. If I set the delete rule to "No Action" for notes, then all notes will not be deleted.

所以我想,是可以临时更改删除更新时用户注释的规则,以便它不会被旧的条目删除?

So I was thinking, is it possible to temporarily change the delete rule of the user note while updating so that it doesn't get deleted with the old entry?

或者我的方法是完全错误,有更好的方法处理这?

Or maybe my approach is completely wrong, and there are better ways to handle this?

更新:我在这里创建了一个跟进问题:更改NSManagedObject与不同上下文的关系

UPDATE: I have created a follow up question here: Change relationship of NSManagedObject to different context

推荐答案

在实例化之后更改模型,除了版本控制。
我认为,你应该创建新的实体,比如说,CustomNote和存储一些唯一的标识符到原始的记录。然后只是通过id检索这个笔记。
虽然,它可能是一些更先进的方法与关系,这是最简单的。

You are not allowed to change model after it was instantiated, except versioning. The way I think, you should create new entity, say, CustomNote and store some unique identifier to original "record". Then just retrieve this notes by id. Although, it may be some more advanced approach with relationships, this is the simplest.

这篇关于核心数据:以编程方式更改删除规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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