在删除后恢复核心数据模型对象时收到通知的正确方法是什么?
我使用 Core Data 设置了一个优雅的系统,每当模型对象的属性发生更改时,它都会使用键值观察自动反映在其关联视图中,但我在使用撤消时遇到了问题。
当我删除模型对象时会出现问题。发生这种情况时,关联的视图以及所有键值观察都会被销毁。用户突然认为删除是一个坏主意,并发出恢复模型对象的撤消命令。此时,键值观察已被破坏,我似乎无法找到一种很好的方法来找出哪个模型对象已起死回生并重新设置所有内容。
我当前想到的解决方案是注册 NSUndoManagerDidUndoChangeNotification,然后手动浏览我的核心数据模型对象并查看哪些对象没有关联的视图。我认为必须有一种方法可以知道哪个特定对象已被带回来,但我认为这种方法有点矫枉过正。
我还考虑过创建一个撤消组,当模型对象重新出现时,会重新添加删除的视图,但只有在可能的情况下,我才希望使撤消管理器与模型相关。
我想我正在寻找的解决方案是让撤消管理器说:“嘿!任何有兴趣的人都听着!我刚刚做了一个撤消,这是已经复活的核心数据模型对象!用它做你想做的事!”然后我设置视图,就像创建了一个新的模型对象一样。
有什么想法或指导吗?
I've got an elegant system set up using Core Data where any time a property of a model object is changed it is automatically reflected in its associated view using key-value observing but I have ran into a problem using undo.
The problem occurs when I have deleted a model object. The associated view is destroyed along with all the key-value observing when this occurs. The user suddenly decides that the deletion was a bad idea and issues an undo command restoring the model object. At this point the key-value observing has been destroyed and I can't seem to find a nice way to figure out which model object has been brought back from the dead and set everything up again.
The current solution I've thought of is registering for the NSUndoManagerDidUndoChangeNotification and then manually going through my Core Data model objects and seeing which ones do not have an associated view. I figure there must be a way to just know which particular object has been brought back though and thought this approach would be overkill.
I've also thought about creating an undo group where the removed view is re-added when the model object reappears but I would like to keep my undo manager related to the model only if that is possible.
I guess the solution I'm looking for is having the undo manager say, "Hey! Anybody who is interested listen up! I just did an undo and here is the Core Data model object that has been resurrected! Do with it what you will!" and then me setting up the view as if a new model object has been created.
Any ideas or guidance?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
NSManagedObject 可能是再次设置观察的正确点:
在撤消/重做操作后将其发送到 NSManagedObject
The following method of NSManagedObject could be the the right point to setup observings again:
It is sent to a NSManagedObject after undo/redo operations
我不确定你的意思,但视图不应该如此直接地与模型绑定,以至于当模型删除某些内容时视图对象本身就会消失。控制器应该处理这个问题并且应该能够逆转它。
听起来您需要为 撤消管理器通知。这至少会让您知道撤消何时执行,然后您可以采取适当的操作。
I'm not sure what you mean by that but a view should not be so directly tied to the model that the view object itself dies when the model deletes something. The controller should be handling that and should be able to reverse it.
It sounds like you need to register the controller for one of the undo manager notifications. That will at least let you know when an undo has been performed and then you can take appropriate action.