开发者

In Core Data is there any consequence to not saving a newly created NSManagedObject into its MOC?

I am creating a NSManagedObject by using the insertNewObjectForEntityForName convience message on NSEntityDescription.

For example:

Person *per = (Person *)[NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:managedObjectContext];

I understand that this will return an autoreleased Person object of instance NSManagedObject to me.

I then decide due to error processing that I don't want to continue to save this new object into the Managed O开发者_StackOverflowbject Context.

  • Do I need to do anything further or just not save and leave it be dealloc'd by the run loops auto release pool?

  • Will this leave the MOC in a stable state? Or should I only use insertNewObjectForEntityForName when I know for sure that I'm going to save the object?


You should only really insert the new object when you know you need it and in this case you would need to delete the object otherwise next time the context IS saved, it will also save this new object.

When calling delete object, if the object has not been saved it will be discarded immediately.

Another approach I take is to create the entity will a nil context (you alloc and init the NSManagedObject manually) and then if you need it you can insert it to your context and save, if not, simply release the object and it is removed immediately.

NOTE: when you insert an object into a context IT DOES NOT automatically insert it's child objects on that context so you need to ensure you go through any referenced objects and insert them on the new context also. For simple primitive types this is not required, only for relationships.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜