János
János

Reputation: 35050

How can a custom NSManagedObject be created without store it in context?

How can I create a custom managed object, but without to save, just keep it in the memory and when app stops, temporary managed object can be dealloched too. But same time other managed objects I need to save.

Upvotes: 0

Views: 85

Answers (1)

Tom Harrington
Tom Harrington

Reputation: 70946

There are a couple of possibilities depending on how your app works.

One is to just create the object and just not insert it. It's just that simple. Pass a nil value for the context.

NSManagedObjectModel *managedObjectModel =
    [[self.managedObjectContext persistentStoreCoordinator] managedObjectModel];
NSEntityDescription *entity = [[managedObjectModel entitiesByName] objectForKey:@"EntityName"];
NSManagedObject *myObject = [[NSManagedObject alloc] initWithEntity:entity insertIntoManagedObjectContext:nil];

If you later want to insert the object, use [NSManagedObjectContext insertObject:].

Another is to create an in-memory Core Data store. Create a second persistent store, but replace NSSQLiteStoreType with NSInMemoryStoreType. Then create and use objects as usual. When the app exits, the in-memory store will just disappear with all of its objects.

Upvotes: 1

Related Questions