Store But Don't Save NSManagedObject to CoreDa

2019-02-22 06:44发布

问题:

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];.