如何回滚临时上下文的变化?
我创build一个像这样的temporaryContext:
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 })
但它没有效果,父上下文不会回滚,为什么?
当你调用rollback
它只会在这个上下文中恢复未保存的更改。 在第一个代码块中,您已经保存了这些更改,因此rollback
将不会执行任何操作。
当你在第一个代码块中调用save
,所有的改变都被提交给父上下文,我认为这是上下文中的主要上下文。 由于您尚未调用主上下文中的save
,因此您应该仍然可以在主上下文中调用rollback
来删除主上下文中的这些更改。