从多个实体中恢复一个获取的实体——核心数据

发布于 2024-11-02 20:13:28 字数 775 浏览 1 评论 0原文

这个问题让我对如何使用核心数据撤消管理器以及如何在编辑完成之前将 NSManagedObject 恢复到其状态缺乏了解。

我正在学习核心数据的方法。我已经使用动态访问器设置了 NSManagedObject 类。我执行的提取返回多个 NSManagedObject 实体结果。每个实体结果(名字、姓氏)的内容都会放入表格视图中,然后用户从表格中选择一个进行详细查看,然后进行编辑。

详细视图控制器接收指向所选 NSManagedObject 实体的指针。当用户编辑字段时,NSManagedObject 实体中相应的属性值也会更新。这似乎是管理这些变化的最干净的方式。

现在,我不想使用保存来提交更改,而是想提供取消编辑功能,该功能可以回滚到该实体的数据库中的内容。我真的只想恢复一个实体而不是执行整个重新获取。

我尝试了回滚,并尝试了 NSUndoManager (使用 beginUndoGrouping 和 endUndoGrouping),但这不起作用。我认为我不明白回滚真正应该做什么。

但无论如何,我仍然想恢复该单个实体中的属性值(采用惰性方法仅获取所需的内容,即一个实体),以便我的详细视图控制器可以使用正确的信息重新填充其视图。现在它正在使用 NSManagedObject 实体值,其中包含已编辑的值,这些值已被取消。

我想我可以通过创建 NSManagedObject 的副本来开始编辑过程。如果按下取消编辑按钮,我可以将其复制回原件中。 (我什至可以通过移动指针来用副本替换原始副本。但是由于指针实际上已经通过了多个对象,所以我不确定如何管理副本上的保留号。

)还有其他建议吗?

谢谢

This question covncerns my lack of understanding of how to use the core data undo manager and how to restore a NSManagedObject to its state before editing was done.

I am just learning my way around Core Data. I have my NSManagedObject classes set up with their dynamic accessors. I perform a fetch that returns several NSManagedObject entity results. Content from each of these entity results (first name, last name) get put into a table view, and then the user picks one out of the table for detailed view and then editing.

The detail view controller receives a pointer to the selected NSManagedObject entity. As the user edits the fields, the corresponding property value in the NSManagedObject entity is updated. This seemed like the cleanest way to manage these changes.

Now, rather than committing the changes using save, I want to provide a cancel-editing feature that rolls back to what is in the data base for that entity. I really only want to restore the one entity and not perform the entire refetch.

I tried rollback and I tried NSUndoManager (with beginUndoGrouping and endUndoGrouping), and that is not working. I don't think I understand what rollback is really supposed to do.

But in any case, I still want to restore the property values in just that single entity (taking the lazy approach to only fetch what is needed, which is the one entity) so that my detail view controller can refill its view with the correct information. Right now it is using the NSManagedObject entity values, which contain the edited values, which were cancelled.

I suppose I could just start the edit process by creating a copy of the NSManagedObject. If the cancel-editing button is pressed, I could copy it back into the original. (I might even be able to just replace the original with the copy by moving the pointer. But since the pointer has actually been passed through several objects, I'm not sure how to manage the retain number on the copy.)

Does anyone have any other suggestions?

Thanks

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

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

发布评论

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

评论(1

吾家有女初长成 2024-11-09 20:13:28

使用 rollback 应该可以完成你想要的,但我不确定它不能完成什么。这可能是一个实现细节错误。

您可以通过调用上下文的 updatedObjects 来查找已更新但尚未保存的特定托管对象。

Using rollback should accomplish what you want and I'm not sure what it doesn't. It is probably an implementation detail error.

You can find the specific managed object/s that were updated but not yet saved by calling the context's updatedObjects.

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