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

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

问题描述

我有一个用户界面插入一个事务。一旦用户点击一个加号他得到屏幕,我想实例化我的Core Data NSManagedObject实体让用户处理它。然后当用户点击保存按钮时,我将调用保存功能。



,然后到代码:

  transaction =(Transaction *)[NSEntityDescription insertNewObjectForEntityForName:@TransactioninManagedObjectContext:self.managedObjectContext]; 
//即使我不调用save:它会出现在我的表
[self.managedObjectContext save:& error]

我使用NSFetchedResultsController对该表,我看到NSFetchedResultsController是插入一个部分和一个对象到表。



我的想法是如果有一种方式实例化事务NSManagedObject,我可以更新它与保存,直到客户端选择。

解决方案

使用nil MOC存在一个根本问题:不同MOC中的对象不应该相互引用—这可能也适用于关系的一侧具有零的MOC。如果保存会发生什么? (如果你的应用程序的另一部分保存,会发生什么?)



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



您可以在NSPredicate中使用 - [NSManagedObject isInserted] (可能是插入和成功保存)。或者,您可以使用具有相同行为的transient属性(在awakeFromInsert中将其设置为YES,并在willSave中将其设置为NO)。



使用第二个MOC是CoreData是如何假设使用的,但是,如果你的应用程序的另一部分保存。它自动处理冲突检测和解决。当然,你不想创建一个新的MOC每次有变化;如果你不介意UI的某些部分在其他部分看到未保存的更改(MOC间通信的开销可以忽略不计),那么有一个MOC对于未缓存的用户线程的更改可能含糊不清。 p>

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天全站免登陆