如何回滚临时上下文的更改?

时间:2015-01-14 14:28:00

标签: ios multithreading core-data nsmanagedobjectcontext

我创建了一个这样的临时文本:

let temporaryContext = NSManagedObjectContext(concurrencyType: .PrivateQueueConcurrencyType)
temporaryContext.parentContext = Utility.managedObjectContext()
temporaryContext.performBlockAndWait({

    // .. here I have done some changes on temporaryContext

    let success = temporaryContext.save(nil)

    //GUI get updated, GUI use MAIN context 
})

我想回滚更改,所以我这样做:

temporaryContext.performBlockAndWait({                                
    temporaryContext.rollback()
    let success = temporaryContext.save(nil)

    //GUI not get restored to the default variable
})

但它没有效果,父上下文不会回滚,为什么?

1 个答案:

答案 0 :(得分:2)

当您致电rollback时,它只会还原该上下文中未保存的更改。在第一个代码块中,您已经保存了这些更改,因此rollback将不会执行任何操作。

当您在第一个代码块中调用save时,所有更改都提交到父上下文,我认为这是本例中的主要上下文。因为您还没有在主上下文中调用save,所以您仍然可以在主上下文中调用rollback来删除主上下文中的这些更改。

相关问题