« A Chip and a Chair   1-for-8 »

16 May 2014

NSManagedObject Without Inserting 

Ever want to create a Managed Object but not have it be part of the Managed Object Context just yet? Possibly a rare situation, but nevertheless, I found myself needing to do this and was dissappointed to find that most of the CoreData documentation assumes that you will definitely want to persist newly created model objects.

Here’s what I eventually figured out to do:

  NSManagedObjectContext *moc = /* Get managed object context */;
  NSEntityDescription *entity = [NSEntityDescription entityForName:@"ModelName"
      inManagedObjectContext:moc];
  NSModelName *modelInstance = [[NSModelName alloc] initWithEntity:entity
      insertIntoManagedObjectContext:nil];

If you then determine that you do want to persist the object:

  [moc insertObject:modelInstance];

(Then at some point call [moc save:&error])

 



comments powered by Disqus