核心数据。成功保存无效对象

发布于 2024-11-29 21:23:42 字数 639 浏览 1 评论 0原文

我在单例中有属性(作为托管对象)。属性中的该对象可能会被更新(删除,然后使用自定义 Id 创建新对象,与旧对象的 Id 相等),然后我保存上下文,使属性无效,获取新对象并将其分配给属性。

我没有出现错误,一切正常。重新启动应用程序后,我收到“CoreData 无法完成故障”异常。我确实通过 sqliteBrowser 打开了我的数据库,并看到了一个旧对象和新对象。新对象是正确的。旧对象(应该删除的对象)与已删除的对象有关系。

此外,当不同的线程更新对象时,UI 可以访问 ManagedObject 的属性。

它是如何发生的?以及如何防止这种情况发生?我阅读了 coreData 故障排除“CoreData 无法完成故障”。自示例以来我没有发现我的问题。

我对此非常困惑。我有想法,但不确定,这是什么好方法: 如果将属性设置为 id(custom id, as NSNumber)、owerload setter 和 getter,会怎么样? Setter 接收相同的对象,但可以访问自定义 id 并为属性分配 id。 Getter - 通过 id 获取对象,保存在属性中。 Getter 和 setter 将在开始时锁定 NSLock 并在结束时解锁。更新对象的线程也会锁定该锁,直到更新和保存完成。 非常感谢!

聚苯乙烯 我非常抱歉,但由于严重的原因,我通常无法显示我的代码。

I have property(as managedObject) in a singleton. This object in property may be updated(removed, then created new object with custom Id, equal with Id of old object), then I save context, nullify the property, fetch new object and assign this to property.

I didn't have an error, all normally. After restarting application I got "CoreData could not fulfill a fault" exception. I did open my database by sqliteBrowser and seen there an old object and new object. New object is correct. Old object(what should been removed) have relationships with objects, which been removed.

Also, UI may have access to property with managedObject when different thread updates an object.

How it can occurs? And how to prevent this? I read coreData troubleshooting with "CoreData could not fulfill a fault". I didn't found my problem since example.

I very confused about this. I have idea, but not sure, what it good way:
What about if make the property not as managedObject, but as id(custom id, as NSNumber), owerload setter and getter. Setter receive same object, but have access to custom id and assign an id for property. Getter - fetches object by id, saved in property.
Getter and setter will lock the NSLock at start and unlock this at end. Thread, which updates an object locks the lock too, until updating and saving is finished.
Big thanks!

P.S.
I very apologize, but I usually can't show my code by serious reason.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

有木有妳兜一样 2024-12-06 21:23:43

取消设置为托管对象的类的属性不会从上下文或存储中删除托管对象,因为两者都独立于任何其他保留而保留托管对象。该对象将继续存在于内存和存储中,并将维持您为其设置的任何关系,直到上下文删除该对象。

发生“无法履行错误”错误是因为 oldObject 具有指向您已删除的其他一些对象的关系。这是由于不正确的数据模型配置导致的,当删除目标对象时,该配置不会破坏关系。检查您的删除规则。必须将其设置为无操作

要实际删除 oldObject,您必须告诉上下文使用类似以下内容显式删除它:

[myManagedObjectContext deleteObject:oldObject];

... 或设置其他实体的关系以使用 cascade 删除它删除规则。

Nullifying a property of a class set to a managed object will not delete the managed object from the context or the store as both retain the managed object independent of any other retention. The object will continue to exist in memory and the store and will maintain any relationships you set for it until the context deletes the object.

The "cannot fulfill a fault" error is occurring because the oldObject has relationships pointing to some other objects you have deleted. This is caused by an improper data model configuration that does not break the relationship when the target object is removed. Check your delete rules. One must be set to No Action.

To actually remove the oldObject you must either tell the context to delete it explicitly with something like:

[myManagedObjectContext deleteObject:oldObject];

... or set the relationship of the other entity to delete it using a cascade delete rule.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文