有没有办法在不插入 NSManagedObject 的情况下实例化它? [英] Is there a way to instantiate a NSManagedObject without inserting it?

查看:21
本文介绍了有没有办法在不插入 NSManagedObject 的情况下实例化它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户界面来插入交易.一旦用户点击一个加号,他就会看到屏幕,我想实例化我的核心数据 NSManagedObject 实体,让用户在它上面工作.然后当用户点击保存按钮时,我将调用保存功能.

代码如下:

transaction = (Transaction *)[NSEntityDescription insertNewObjectForEntityForName:@"Transaction" inManagedObjectContext:self.managedObjectContext];//即使我不调用保存:它会出现在我的桌子上[self.managedObjectContext save:&error]

P.S 我在那个表上使用 NSFetchedResultsController,我看到 NSFetchedResultsController 正在向表中插入一个部分和一个对象.

我的想法是,如果有一种方法可以实例化事务 NSManagedObject,我可以在不保存的情况下更新它,直到客户选择这样做.

解决方案

使用 nil MOC 有一个基本问题:不同 MOC 中的对象不应该相互引用 —当关系的一方没有 MOC 时,这大概也适用.如果保存会发生什么?(当您应用的另一部分保存时会发生什么?)

如果你的对象没有关系,那么你可以做很多事情(比如 NSCoding).

您也许可以在 NSPredicate 中使用 -[NSManagedObject isInserted] (大概在插入和成功保存之间是 YES).或者,您可以使用具有相同行为的瞬态属性(在awakeFromInsert 中将其设置为YES,在willSave 中将其设置为NO).如果您的应用程序的不同部分保存,这两种方法都可能会出现问题.

不过,使用第二个 MOC 是应该"使用 CoreData 的方式;它会自动为您处理冲突检测和解决方案.当然,您不希望每次发生变化时都创建一个新的 MOC;如果您不介意 UI 的某些部分在其他部分看到未保存的更改(MOC 间通信的开销可以忽略不计),那么通过缓慢的用户线程"为未保存的更改设置一个 MOC 可能是模糊的明智之举.

I have a user interface to insert a Transaction. once the user clicks on a plus he gets the screen and i want to instantiate my Core Data NSManagedObject entity let the user work on it. Then when the user clicks on the Save button i will call the save function.

so down to code:

transaction = (Transaction *)[NSEntityDescription insertNewObjectForEntityForName:@"Transaction" inManagedObjectContext:self.managedObjectContext];
//even if i dont call save: its going to show up on my table
[self.managedObjectContext save:&error]

P.S i am using an NSFetchedResultsController on that table and I see that the NSFetchedResultsController is inserting a section and an object to the table.

My thought is if there is a way to instantiate the Transaction NSManagedObject i could update it with out saving untill the client choses to.

解决方案

There's a fundamental problem with using a nil MOC: Objects in different MOCs aren't supposed to reference each other — this presumably also applies when one side of a relationship has a nil MOC. What happens if you save? (What happens when another part of your app saves?)

If your object doesn't have relationships, then there are plenty of things you can do (like NSCoding).

You might be able to use -[NSManagedObject isInserted] in NSPredicate (presumably it's YES between inserting and successfully saving). Alternatively, you can use a transient property with the same behaviour (set it to YES in awakeFromInsert and NO in willSave). Both of these may be problematic if a different part of your app saves.

Using a second MOC is how CoreData is "supposed" to be used, though; it handles conflict detection and resolution for you automatically. Of course, you don't want to create a new MOC each time there's a change; it might be vaguely sensible to have one MOC for unsaved changes by the slow "user thread" if you don't mind some parts of the UI seeing unsaved changes in other parts (the overhead of inter-MOC communication is negligible).

这篇关于有没有办法在不插入 NSManagedObject 的情况下实例化它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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