ios Coredata 的 rollback undo 等事物处理函数
首先说明 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 等事物处理函数的更多相关文章
- iOS CoreData技术学习资源汇总
一.CoreData学习指引 1. 苹果官方:Core Data Programming Guide 什么是CoreData? 创建托管对象模型 初始化Core Data堆栈 提取对象 创建和修改自定 ...
- IOS CoreData 多表查询demo解析
在IOS CoreData中,多表查询上相对来说,没有SQL直观,但CoreData的功能还是可以完成相关操作的. 下面使用CoreData进行关系数据库的表与表之间的关系演示.生成CoreData和 ...
- iOS CoreData (一) 增删改查
代码地址如下:http://www.demodashi.com/demo/11041.html Core Data是iOS5之后才出现的一个框架,本质上是对SQLite的一个封装,它提供了对象-关系映 ...
- iOS CoreData (二) 版本升级和数据库迁移
前言:最近ChinaDaily项目需要迭代一个新版本,在这个版本中CoreData数据库模型上有新增表.实体字段的增加,那么在用户覆盖安装程序时就必须要进行CoreData数据库的版本升级和旧数据迁移 ...
- IOS CoreData 多表查询(下)
http://blog.csdn.net/fengsh998/article/details/8123392 在iOS CoreData中,多表查询上相对来说,没有SQL直观,但COREDATA的功能 ...
- iOS CoreData 介绍和使用(以及一些注意事项)
iOS CoreData介绍和使用(以及一些注意事项) 最近花了一点时间整理了一下CoreData,对于经常使用SQLite的我来说,用这个真的有点用不惯,个人觉得实在是没发现什么亮点,不喜勿喷啊.不 ...
- iOS CoreData介绍和使用(以及一些注意事项)
iOS CoreData介绍和使用(以及一些注意事项) 最近花了一点时间整理了一下CoreData,对于经常使用SQLite的我来说,用这个真的有点用不惯,个人觉得实在是没发现什么亮点,不喜勿喷啊.不 ...
- iOS面向编码|iOSVideoToolbox:读写解码回调函数CVImageBufferRef的YUV图像
iOS面向编码|iOSVideoToolbox:读写解码回调函数CVImageBufferRef的YUV图像 本文档基于H.264的解码,介绍读写Video Toolbox解码回调函数参数CVImag ...
- iOS CoreData学习资料 和 问题
这里是另一篇好文章 http://blog.csdn.net/kesalin/article/details/6739319 这里是另一篇 http://hxsdit.com/1622 (不一定能访问 ...
随机推荐
- QQ空间HD(4)-设置左侧菜单栏属性
DJHomeViewController.m #import "DJHomeViewController.h" #import "DJMenuView.h" ; ...
- 弹出框四 之toastr.js (完成提示框)
1.下载 toastr.js组件 2. $(function () { toastr.success('提交数据成功'); toastr.error('Error'); toastr.warning( ...
- recording just for inquiry in the future
auditd审计 相关命令有: auditd, auditctl, ausearch, aureport 相关文件: /etc/audit/auditd.conf, /etc/audit/audit. ...
- Windows Phone 开发环境的搭建
1. 系统 系统:Windows 7(32 位).Windows 7(64 位).Windows Vista SP2(32 位)和 Windows Vista(64 位)或者更高版本. 不支持 :Wi ...
- Bus Hound 的使用方法
背景: 最近在研究USB相关协议,需要对USB数据进行抓取分析,Bus Hound是个非常赞的工具,在此将其使用方法记录下来,以备下次快速上手使用. 正文: 主界面如下: 首先关注菜单栏三个选项: C ...
- UI第六节——UINavigationController 详解
1. UINavigationController 是一个容器类.里面盛放的是UIViewController. 容器的意思是,如果你不放入UIViewController,里面就是空的,什么也没有. ...
- 转载自安卓巴士 【收藏】2015必须推荐的Android框架,猿必读系列!
一.Guava Google的基于java1.6的类库集合的扩展项目,包括collections, caching, primitives support, concurrency libraries ...
- View和ViewImage设置图片
1.view类的设置背景android:background --setBackgroundResource(int) --A drawable to use as the background. s ...
- MongoDB的学习和使用(查询find)
语法 MongoDB 查询数据的语法格式如下: >db.COLLECTION_NAME.find() find() 方法以非结构化的方式来显示所有文档. 如果你需要以易读的方式来读取数据,可以使 ...
- Eclipse 语法提示
新建一个txt 拷贝下面的文本,然后保存修改扩展名为.epf #Sat Nov :: CST /instance/org.eclipse.jdt.core/org.eclipse.jdt.core.c ...