Create NSManagedObject without saving?

Possible duplicate:
Save but not save NSManagedObject in CoreData?

I need to do NSManagedObject without saving it, how can I do it?

The reason I want to do this is the application in which the user enters their data, but I want to save the object only if they complete the setup (they have the ability to cancel, in this case the object must be discarded without saving, so I don’t want to embed it right away).

I tried to insert one without context, but the application will work.

I tried the following:

GuestInfo *guest; guest = (GuestInfo *)[NSEntityDescription insertNewObjectForEntityForName:@"GuestInfo" inManagedObjectContext:nil]; 

This crashes with the following error message:

 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'GuestInfo'' 

It causes

Hope you can help, thanks.

+6
source share
4 answers

I would recommend creating a managed entity and pasting it into the managed entity context as usual. You will have a reference to the managed entity, i.e.:

 GuestInfo* guest = (GuestInfo *)[NSEntityDescription insertNewObjectForEntityForName:@"GuestInfo" inManagedObjectContext:managedObjectContext]; 

Then, if the user cancels, simply remove it from the context of the managed entity as follows:

 [guest deleteInContext:managedObjectContext]; 

The context of managed objects is designed as a notebook for creating and deleting objects in it as follows.

Another option you can consider is to call:

 [managedObjectContext rollback] 

if the user cancels. those. you will create a managed entity in the context of the managed entity, but if the user cancels, you will cancel or cancel the state of the context of the managed entity as it was the last time it was saved. See the "Canceling Management" section of Apple's Use of Managed Objects document:

https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/CoreData/Articles/cdUsingMOs.html

+11
source

Create an NSManagedObjectContext as a child of your regular context. You can make all the changes there that you want, and until you call "Save", material that will not be clicked there.

For instance...

 NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] initWithConcurrencyType: NSPrivateQueueConcurrencyType]; moc.parentContext = myCurrentManagedObjectContext; 

Now from any thread in your program you can make the next call ...

 [moc performBlock:^{ // Do anything you want to with this context... make a new object, whatever. // As long as you do not call [moc save], your changes will not propagate // up to the parent context, and they obviously will they be saved either. }]; 
+5
source

It’s a little strange to create a Core Data object if you don’t want to save it .... but in any case, the error says that you do not have a model file called GuestInfo included in your project. Make sure it exists and is included in the Copy Resources section. A model file is one where you declare types in your database and the connections between them.

0
source

Managed objects must always have context. If you do not want the objects to be persistent, just do not save the context.

If you never want your objects to be persistent, it is doubtful if you really should use Core Data.

0
source

Source: https://habr.com/ru/post/913271/


All Articles