Store But Don't Save NSManagedObject to CoreData?
Asked Answered
T

2

6

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?

Tiffanitiffanie answered 15/4, 2012 at 18:34 Comment(0)
G
3

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'.

Geddes answered 15/4, 2012 at 18:38 Comment(4)
Well thats where I am having problems. I am never calling save on the context, I could even enter no details whatsoever, leave all fields blank in the detail view and go back to my list of object and see an object has been added despite this (with null values).Tiffanitiffanie
That said, I think I just found what I was doing wrong. Besides that though, I need to pass my object down about 5 views (which isn't the most favourable thing to do) how can I easily do this?Tiffanitiffanie
If each of the intermediate view controllers will interact with the object, it makes sense to simply pass the object to each consecutive view controller (I'm assuming you're using a nav controller stack) by way of a property. If views 2-4 have nothing to do with the object, than you might consider making your 5th view controller a delegate of your first (where you are obtaining the original managed object reference).Geddes
Ok cool, looks like I'll be setting up a delegate. Hoping there would be a super duper simple way like just holding it in NSUserDefaults for a short time, but Im expecting that would be too sluggish? Anyway, thanks.Tiffanitiffanie
B
12

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

Backstretch answered 15/4, 2012 at 18:46 Comment(0)
G
3

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'.

Geddes answered 15/4, 2012 at 18:38 Comment(4)
Well thats where I am having problems. I am never calling save on the context, I could even enter no details whatsoever, leave all fields blank in the detail view and go back to my list of object and see an object has been added despite this (with null values).Tiffanitiffanie
That said, I think I just found what I was doing wrong. Besides that though, I need to pass my object down about 5 views (which isn't the most favourable thing to do) how can I easily do this?Tiffanitiffanie
If each of the intermediate view controllers will interact with the object, it makes sense to simply pass the object to each consecutive view controller (I'm assuming you're using a nav controller stack) by way of a property. If views 2-4 have nothing to do with the object, than you might consider making your 5th view controller a delegate of your first (where you are obtaining the original managed object reference).Geddes
Ok cool, looks like I'll be setting up a delegate. Hoping there would be a super duper simple way like just holding it in NSUserDefaults for a short time, but Im expecting that would be too sluggish? Anyway, thanks.Tiffanitiffanie

© 2022 - 2024 — McMap. All rights reserved.