首先说明 ios 中 NSManagedObjectContext 默认的 undoManager是nil的,就是说 undo 和 redo 都是没用的。

但是 rollback函数和reset函数是不设置undoManager对象也能使用的!

看下rollback的说明

Removes everything from the undo stack, discards all insertions and deletions, and restores updated objects to their last committed values.

NSManagedObjectContext的undo和redo都是调用undoManager的undo和redo,在很多情况下,undo一组没有save的操作和rollback整个context是一样的效果---都把context恢复到了上次save后的状态。但是undoManager如果进行了更加复杂的设定,undo就仅仅是恢复一部分数据库操作,而不像rollback那么彻底了!

在 NSManagedObject 的awakeFromInsert函数中找到了以下说明:

If you create a managed object then perform undo operations to bring the managed object context to a state prior to the object’s creation, then perform redo operations to bring the managed object context back to a state after the object’s creation, awakeFromInsert is not invoked a second time.

这里说明了 context中的 undo的一个用处,就是取消一个插入对象!

为了理解 undomanager,贴一段简单的例子:

#import "ViewController.h"

@interface ViewController ()

{

    NSUndoManager *undoManager;
float width;
} @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. undoManager = [[NSUndoManager alloc] init]; width = 1.0; [self setMyWidth:2.0]; if([undoManager canUndo]){
NSLog(@"can undo");
} [undoManager undo]; if([undoManager canUndo]){
NSLog(@"can undo");
} [undoManager redo]; NSLog(@"width is %f",width); } - (void)setMyWidth:(float)newWidth{ if(undoManager.isUndoing){
NSLog(@"is undoing");
} if(undoManager.isRedoing){
NSLog(@"is redoing ");
} float currentWidth = width; if ((newWidth != currentWidth)) {
[[undoManager prepareWithInvocationTarget:self] setMyWidth:currentWidth]; [undoManager setActionName:NSLocalizedString(@"Size Change", @"size undo")]; width = newWidth;
}
} @end

这个例子里的undoManager的操作,coredata都已经实现了,我们不需要写

- (void)setMyWidth:(float)newWidth

这样的函数了。

ios Coredata 的 rollback undo 等事物处理函数的更多相关文章

  1. iOS CoreData技术学习资源汇总

    一.CoreData学习指引 1. 苹果官方:Core Data Programming Guide 什么是CoreData? 创建托管对象模型 初始化Core Data堆栈 提取对象 创建和修改自定 ...

  2. IOS CoreData 多表查询demo解析

    在IOS CoreData中,多表查询上相对来说,没有SQL直观,但CoreData的功能还是可以完成相关操作的. 下面使用CoreData进行关系数据库的表与表之间的关系演示.生成CoreData和 ...

  3. iOS CoreData (一) 增删改查

    代码地址如下:http://www.demodashi.com/demo/11041.html Core Data是iOS5之后才出现的一个框架,本质上是对SQLite的一个封装,它提供了对象-关系映 ...

  4. iOS CoreData (二) 版本升级和数据库迁移

    前言:最近ChinaDaily项目需要迭代一个新版本,在这个版本中CoreData数据库模型上有新增表.实体字段的增加,那么在用户覆盖安装程序时就必须要进行CoreData数据库的版本升级和旧数据迁移 ...

  5. IOS CoreData 多表查询(下)

    http://blog.csdn.net/fengsh998/article/details/8123392 在iOS CoreData中,多表查询上相对来说,没有SQL直观,但COREDATA的功能 ...

  6. iOS CoreData 介绍和使用(以及一些注意事项)

    iOS CoreData介绍和使用(以及一些注意事项) 最近花了一点时间整理了一下CoreData,对于经常使用SQLite的我来说,用这个真的有点用不惯,个人觉得实在是没发现什么亮点,不喜勿喷啊.不 ...

  7. iOS CoreData介绍和使用(以及一些注意事项)

    iOS CoreData介绍和使用(以及一些注意事项) 最近花了一点时间整理了一下CoreData,对于经常使用SQLite的我来说,用这个真的有点用不惯,个人觉得实在是没发现什么亮点,不喜勿喷啊.不 ...

  8. iOS面向编码|iOSVideoToolbox:读写解码回调函数CVImageBufferRef的YUV图像

    iOS面向编码|iOSVideoToolbox:读写解码回调函数CVImageBufferRef的YUV图像 本文档基于H.264的解码,介绍读写Video Toolbox解码回调函数参数CVImag ...

  9. iOS CoreData学习资料 和 问题

    这里是另一篇好文章 http://blog.csdn.net/kesalin/article/details/6739319 这里是另一篇 http://hxsdit.com/1622 (不一定能访问 ...

随机推荐

  1. H5 使用

    关闭页面 http://www.bcty365.com/content-146-3343-1.html 回退页面: plus.key.addEventListener('backbutton', fu ...

  2. 安装使用ubuntu和opensuse

    liquid: n.液体, adj. 液体的, 流动的 liquidate: v. 清洗; 清算; 变现; 杀戮; weird: [wi2d] e:i, ir:2 离奇的,古怪的... 英文名称, 直 ...

  3. CF461B Appleman and Tree (树DP)

    CF462D Codeforces Round #263 (Div. 2) D Codeforces Round #263 (Div. 1) B B. Appleman and Tree time l ...

  4. hdu4888 Redraw Beautiful Drawings 最大流+判环

    hdu4888 Redraw Beautiful Drawings Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/6553 ...

  5. 点击每个li输出里面的内容(前端很常问的面试题之一)

    点击每个li输出里面的内容(前端很常问的面试题之一) 前端 面试 JavaScript <!DOCTYPE html> <html lang="en"> & ...

  6. JavaScript模板引擎实现数据交互

    经过1年的磨练,近期终于稍微明白到,前端是怎么做到企业要求的:数据交互. 1,ajax+json这个是必须学的,但没问题,我们可以通过这个博客来慢慢了解怎么回事? 2,可以通过JS框架和JS模板来实现 ...

  7. Sphinx扩展安装安装

    Coreseek官方教程中建议php使用直接include一个php文件进行操作,事实上php有独立的sphinx模块可以直接操作coreseek(coreseek就是sphinx!)已经进入了php ...

  8. asp.net—缓存

    1.页面缓存 要实现页面输出缓存,只要将一条 OutputCache 指令添加到页面即可. <%@ OutputCache CacheProfile=" " NoStore= ...

  9. HDOJ 4389 X mod f(x)

    数位DP........ X mod f(x) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/ ...

  10. Linux 下复制(cp)目录时排除一个或者多个目录的方法

    cp 貌似没有排除目录的功能,可以使用 rsync 命令来实现了,如: [案例] /home/52php目录里面有data目录,data目录里面有 a.b.c.d.e 五个目录,现在要把data目录里 ...