如何恢复 CoreData NSManagedObjectContext 中 NSUndoManager 的内容?

发布于 2024-08-16 19:43:06 字数 276 浏览 3 评论 0原文

我想在 CoreData (NSManagedObject) 对象上的 iPhone 应用程序中使用 NSUndoManager,这样如果应用程序过早退出(例如,由于接到电话),我可以保存(并稍后恢复)NSUndoManager 的状态。即,与自动丢弃或保存 NSUndoManager 中累积的更改相反,我想恢复它们,以便用户在重新启动应用程序时可以选择显式丢弃或保存它们。

有人有这方面的经验吗?当应用程序中断时,任何人都可以推荐这种(或替代)方法来管理 NSManagedObjectContext 中的挂起更改吗?

I'd like to use NSUndoManager in an iPhone application on CoreData (NSManagedObject) objects such that I can save (and later restore) the state of the NSUndoManager if the application exits prematurely (say, due to a phone call coming in). I.e. as opposed to automatically discarding or saving the changes accumulated in the NSUndoManager, I would like to restore them so that the user has the option to explicitly discard or save them when they restart the app.

Has anyone had any experience with this? Can anyone recommend this (or an alternative) approach to managing pending changes in a NSManagedObjectContext when the application is interrupted?

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

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

发布评论

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

评论(2

阳光的暖冬 2024-08-23 19:43:06

NSUndoManager 实际上并不存储状态,它存储一组将恢复状态的操作。例如,如果您有一个对象 XXX,它有一个字符串属性名称,并且您将该名称从“Steve”更改为“Joe”,则 NSUndoManager 存储的是目标、选择器和对象。目标将是 XXX 的实例,选择器将是 @selector(setName:),对象将是 @"Steve"

通过存储该信息,如果撤消堆栈被弹出,它将在对象 XXX 的实例上调用值为 @"Steve" 的 -setName: ,从而恢复其状态。围绕 KVO 等还做了一些额外的工作,但这只是基础知识。

起初,我推测您可以将 NSManagedObjectID、选择器(使用 NSStringFromSelector)和对象写入磁盘,并通过调用 -registerUndoWithTarget: 选择器来恢复它们:对象:。然而,在进一步查看文档后,我们发现无法访问堆栈以对其进行迭代。

The NSUndoManager does not actually store state, it stores a stack of actions that will restore the state. For example, if you have an object XXX and it has a property name which is a string and you change that name from "Steve" to "Joe", what the NSUndoManager stores is a target, selector and object. The target would be the instance of XXX, the selector would be @selector(setName:) and the object would be @"Steve".

By storing that information, if the undo stack is popped it will call -setName: on the instance of object XXX with the value of @"Steve" and thus restoring its state. There is some additional work done around KVO, etc. but that is the basics.

At first I theorized that you could write out the NSManagedObjectID, the selector (using NSStringFromSelector) and the object to disk and restore them by calling -registerUndoWithTarget: selector: object:. However upon further review of the documentation, there is no way to access the stack to be able to iterate over it.

空宴 2024-08-23 19:43:06

请注意,存在一种可能的解决方法,即使用单独的 NSManagedObjectContext,以便某些在关闭时保存,而另一些则回滚其更改。它并不完美,但我通过这种替代方案找到了解决我的问题的合适方法。

Note that one possible work-around exists by using separate NSManagedObjectContexts such that some are saved on shutdown whereas others have their changes rolled back. It's not perfect, but I found a suitable solution to my problem with this alternative.

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