NSManagedObjectContext 重置和回滚有什么区别? [英] what's the difference between NSManagedObjectContext reset and rollback?

查看:15
本文介绍了NSManagedObjectContext 重置和回滚有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档说:

<块引用>

- (void)reset

将接收器返回到其基本状态.

讨论

接收者的所有管理对象都被遗忘".如果您使用此方法,您应该确保还丢弃对使用接收器获取的任何托管对象的引用,因为它们之后将无效.


<块引用>

- (void)rollback

从撤消堆栈中删除所有内容,丢弃所有插入和删除,并将更新的对象恢复到它们最后提交的值.

讨论

此方法不会从一个或多个持久存储中重新获取数据.

似乎在我对我的上下文进行一些更改之后,调用这两个方法将做完全相同的事情:丢弃更改并将更新的对象恢复到它们上次提交的值.那么 -reset 实际上是做什么的?

解决方案

关键部分在引用中

<块引用>

所有接收者的管理对象都被遗忘"了.

- (void)reset; 会给你一个干净的 NSManagedObjectContext ,里面没有任何对象,并且文档说明了任何 NSManagedObject你周围的东西应该被丢弃,因为它们不再有效.

- (void)rollback 只会将 NSManagedObject 恢复到它们的持久值

The documentation says:

- (void)reset

Returns the receiver to its base state.

Discussion

All the receiver's managed objects are "forgotten." If you use this method, you should ensure that you also discard references to any managed objects fetched using the receiver, since they will be invalid afterwards.


- (void)rollback

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

Discussion

This method does not refetch data from the persistent store or stores.

It seems that after I make some change to my context, calling these two method will do exactly the same thing: discard changes and restore updated objects to their last committed values. So what does -reset actually do?

解决方案

The key part is in the quote

All the receiver's managed objects are "forgotten".

- (void)reset; will give you a clean NSManagedObjectContext with no objects in it and as the docs state any NSManagedObject's you have around should be discarded as they are no longer valid.

- (void)rollback will just restore the NSManagedObject's to their persisted values

这篇关于NSManagedObjectContext 重置和回滚有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆