创建NSManagedObject而不保存? [英] Create an NSManagedObject Without Saving?

查看:149
本文介绍了创建NSManagedObject而不保存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

Store但不要将NSManagedObject保存到CoreData中?




$ b b

我需要创建一个NSManagedObject而不保存它,我该如何做?

I need to make an NSManagedObject without saving it, how can I do this?

我想这样做的原因是应用程序有一个设置,其中用户输入他们的详细信息,但我只想保存对象,如果他们完成设置(他们有选项取消,在这种情况下对象需要被丢弃而不保存,这就是为什么我不想插入

The reason I want to do this is the app has a setup in which the user enters their details, however I only want to save the object if they complete the setup (they have the option to cancel, in which case the object needs to be discarded without being saved, which is why I don't want to insert it straight away).

我已尝试在没有上下文的情况下插入一个上下文,但应用崩溃。

I have tried insetting one without a context but the app crashes.

尝试以下操作:

GuestInfo *guest;
guest = (GuestInfo *)[NSEntityDescription insertNewObjectForEntityForName:@"GuestInfo" inManagedObjectContext:nil];

这会导致崩溃,并显示以下错误消息:

This causes the crash with the following error message:

'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'GuestInfo''

这会导致

希望你能帮助。

推荐答案

我建议创建托管对象和插入到托管对象上下文正常。您将引用受管对象,即:

I would recommend creating the managed object and inserting into your managed object context as normal. You will have a reference to the managed object, i.e.:

GuestInfo* guest = (GuestInfo *)[NSEntityDescription insertNewObjectForEntityForName:@"GuestInfo" inManagedObjectContext:managedObjectContext];

然后,如果用户取消,只需从托管对象上下文中删除它:

Then if the user cancels, just delete it from the managed object context like this:

[guest deleteInContext:managedObjectContext];

托管对象上下文设计为一个暂存器,可以像这样创建和删除对象。

The managed object context is designed as a scratchpad for you to create and delete objects in it like this.

您可能考虑的另一个选择是调用:

Another option you might consider is calling:

[managedObjectContext rollback]

即您将在受管对象上下文中创建受管对象,但是如果用户取消,则可以撤消或回滚受管对象上下文的状态,使其与上次保存时的状态保持一致。请参阅Apple的使用托管对象文档中的撤消管理部分:

if the user cancels. i.e. you would create the managed object in the managed object context, but if the user cancels, you undo or rollback the state of the managed object context to how it was at the last time it was saved. See the "Undo Management" section of the Apple's "Using Managed Objects" doc:

https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/CoreData/Articles/cdUsingMOs.html

这篇关于创建NSManagedObject而不保存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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