ios – 核心数据 – 保存上下文后撤消更改

我最近注意到Core Data中的撤销机制这个奇怪的事情,从那以后它一直困扰着我.

引用来自NSManagedObjectContext文档的-undo方法:

Sends an undo message to the receiver’s undo manager,asking it to reverse the latest uncommitted changes applied to objects in the object graph.

要扭转最新的未提交更改,听起来很简单吧?

但是,实际情况并非如此!即使我使用托管对象上的更改保存上下文,以下-undo调用仍将成功撤消更改.这不是针对文档中陈述的内容吗?

也许我做错了什么?如果需要,我可以发布我的小测​​试代码.我真的很困惑.

解决方法

你应该感到困惑.核心数据文档是一个热点.他们使用了许多像“未提交”这样的词,这些词可能是不合适的.它们似乎意味着当它们说“未提交”时,其属性isFaulted等于NO的对象.

核心数据编程指南更详细:

Change and Undo Management

A context keeps strong references to managed objects that have pending
changes (insertions,deletions,or updates) until the context is sent
a save:,reset,rollback,or dealloc message,or the appropriate
number of undos to undo the change.

The undo manager associated with a context keeps strong references to
any changed managed objects. By default,in OS X the context’s undo
manager keeps an unlimited undo/redo stack. To limit your
application’s memory footprint,you should make sure that you scrub
(using removeAllActions) the context’s undo stack as and when
appropriate
. Unless you keep a strong reference to a context’s undo
manager,it is deallocated with its context.

文档中的措辞/词汇不清晰或不一致.我相信,在适合您的应用程序时,您应该在上下文的undoManager属性上调用removeAllActions,以避免无限的内存增长.

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...