Store But Don't Save NSManagedObject to CoreData?

只愿长相守 提交于 2019-12-19 18:29:18

问题


I have a short setup process in my app and I create an NSManagedObject to store name and other details of an individual, however during this setup I don't want to save the object until the user presses 'Done' right at the end of the setup (just incase they cancel the setup for whatever reason).

So is it possible to keep hold the object holding my info for a short time until the end of the setup process without actually saving it into CoreData?


回答1:


When you are dealing with CoreData, all add/modify/delete actions occur in the NSManagedObjectContext - but changes are not persisted to disk until you call 'save' on that context.

So the answer is yes - this is the behavior you should already be getting. If you are adding or modifying the properties of NSManagedObjects, these changes are kept in the memory of the context, but are not being saved to disk until you actually call 'save'.




回答2:


You can also use

NSEntityDescription *ed = [NSEntityDescription entityForName:@"YourManagedObject" inManagedObjectContext:managedObjectContext];
YourManagedObject *obj = [[[YourManagedObject alloc] initWithEntity:ed insertIntoManagedObjectContext:nil] autorelease];

to create the managed object without inserting it into your context. You can do that later by calling [managedObjectContext insertObject:obj];.



来源:https://stackoverflow.com/questions/10164850/store-but-dont-save-nsmanagedobject-to-coredata

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!