zoukankan      html  css  js  c++  java
  • 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

    这样的函数了。

  • 相关阅读:
    wpf读取mysql字段类型为text的问题
    设计模式简介
    为 RESTful API 配置 CORS 实现跨域请求
    js FileReader 读取文件
    js读取文件fileReader
    制作svg动态图形效果
    H5与Native交互之JSBridge技术
    位(bit)、字节(byte)、字符、编码之间的关系
    node.js的net模块实现socket通信
    Flexbox如何将页面底部固定在屏幕最下方
  • 原文地址:https://www.cnblogs.com/breezemist/p/5130671.html
Copyright © 2011-2022 走看看