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

前端之家收集整理的这篇文章主要介绍了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相关文章