撤消核心数据管理对象 [英] Undo core data managed object

查看:120
本文介绍了撤消核心数据管理对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码:

Store* store = [NSEntityDescription insertNewObjectForEntityForName:@"Store"];
store.name = @"My Company"
...

现在存储在上下文中管理,并在保存上下文时保存,但我有一个按钮,用户可以在其中取消收集数据的表单。如何从上下文中撤消或删除它?或者我认为错了?

Now the store is managed in the context and will be saved when the context is saved, but I have a button where the user can cancel the form where data is collected. How do I undo or remove this from the context? Or am I thinking wrong?

推荐答案

如前所述,您可以使用撤消管理器。或者,您可以简单地使用单独的ManagedObjectContext,并在那里进行所有更改。如果您决定保留它们,请保存上下文。如果没有,只需丢弃它。 MOC只是工作的暂存区,在保存之前对底层数据库没有影响。

As mentioned earlier, you can use an undo manager. Or, you could simply use a separate ManagedObjectContext, and do all your changes in there. If you decide to keep them, save the context. If not, simply discard it. A MOC is just a scratch pad for work, and has no impact on the underlying database until saved.

你不能真正分离一个实体但你可以导致一个托管对象,可以变回故障,丢失任何尚未保存的更改。

You can't really "detach an entity" but you can cause a managed object to turn back into a fault, losing any changes that have not been saved.

[managedObjectContext refreshObject:object mergeChanges:NO];

从文档中删除...


如果标志为NO,则对象变为故障,任何挂起的
更改都将丢失。该对象仍然是一个错误,直到再次访问
,此时它的属性值将从
商店或最后一个缓存状态重新加载。

If flag is NO, then object is turned into a fault and any pending changes are lost. The object remains a fault until it is accessed again, at which time its property values will be reloaded from the store or last cached state.

这篇关于撤消核心数据管理对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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