来自临时 URI 的永久 NSManagedObject URI?

发布于 2024-10-09 07:31:06 字数 836 浏览 6 评论 0原文

我有一个 NSManagedObject 子类,它存储子对象的 URI。它最初会在子对象首次创建时存储临时 URI,并在保存之前实现 -willSave 在其 NSManagedObjectContext 上调用 -obtainPermanentIDsForObjects,以便仅保留永久 ID 。

不过,我遇到的问题是其他实例也存储临时 URI。当一个对象在具有相同子对象的另一个对象收到 -willSave 消息后不久收到时,它也会调用 -obtainPermanentIDsForObjects 并得到一个不同的、错误 永久 URI,导致后续的核心数据故障错误,例如:

CoreData 无法满足 '0x102e4c110'

我想要一种从 NSManagedObject 的旧的、临时的 URIRepresentation 到已生成的新的、永久的 URIRepresentation 的方法。

我最初的想法是存储我自己的 字典,并在使用 -obtainPermanentIDsForObjects 之前进行查找 - 是否有更简单的(更简单或更简单的)内置)我缺少的方式?我在文档中没有发现任何暗示。

I have an NSManagedObject subclass which stores URIs of child objects. It initially stores the temporary URI when a child object is first created, and implements -willSave to call -obtainPermanentIDsForObjects on its NSManagedObjectContext before saving, so that only the permanent ID is persisted.

The problem I'm running into, though, is that other instances also store the temporary URI. When one gets the -willSave message shortly after another object with the same child does, it also calls -obtainPermanentIDsForObjects and gets a different, wrong permanent URI, causing subsequent Core Data fault errors like:

CoreData could not fulfill a fault for '0x102e4c110 <x-coredata://E17EE19B-E7F3-4102-ACFF-3E6F8BE8B104/MyEntityName/p2>'

I would like a way to get from an NSManagedObject's old, temporary, URIRepresentation to its new, permanent, URIRepresentation that was already generated.

My initial thought is to store my own dictionary of <temporary URI,permanent URI> and look up against that before using -obtainPermanentIDsForObjects - is there an easier (more foolproof or built-in) way that I'm missing? Nothing I found in the documentation suggested anything.

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

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

发布评论

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

评论(2

夢归不見 2024-10-16 07:31:06

我最终在创建子对象后(在它属于任何父对象之前)立即通过 -obtainPermanentIDsForObjects 调用获得了永久 ID。这样,父对象就永远不会看到临时 ID。这是可行的,尽管我仍然渴望听到是否有更好的解决方案。

I ended up getting a permanent ID with the -obtainPermanentIDsForObjects call immediately after creating the child object, before it belongs to any parent. That way, the parent object never sees a temporary ID. This works, though I'm still anxious to hear if there are any better solutions.

心凉怎暖 2024-10-16 07:31:06

订阅托管对象上下文的 NSManagedObjectContextDidSaveNotification。遍历 NSInsertedObjectsKey 中的对象并获取它们的托管对象 ID。此时,它们都将是正确的、永久的 ID,您可以安全地获取 URI 并存储它。

Subscribe to your managed object context's NSManagedObjectContextDidSaveNotification. Iterate through the objects in the NSInsertedObjectsKey and get their managed object ids. At this point, they will all be the correct, permanent id and you can safely get the URI and store it.

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