title: iOS 8 – Creating temporary NSManagedObjects
body: <p>In iOS 7 (and earlier), there was the ability to effectively create "temporary" <code>NSManagedObject</code>s with the <em>option</em> to later add it to a context and persist it, like so:</p>
  
  <pre><code>NSEntityDescription* entityDescription = [NSEntityDescription entityForName:@"User" inManagedObjectContext:managedObjectContext];
  User* user = [[User alloc] initWithEntity:entityDescription insertIntoManagedObjectContext:nil];
  </code></pre>
  
  <p>Note the <code>nil</code> NSManagedObjectContext parameter. (Check out Marcus S. Zarra's answer on this method <a href="https://stackoverflow.com/a/3258470/1236057">here</a>)</p>
  
  <p>However, iOS 8 has changed how relationships are managed, such that if you create a temporary object and add a relationship to it before setting its context, the relationship will be deleted upon relaunch:</p>
  
  <pre><code>User* user = [User temporaryEntity];
  [user addPhotosObject:photo];
  [managedObjectContext:insertObject:user];
  [managedObjectContext:&amp;error]; 
  </code></pre>
  
  <p>This doesn't affect non-relational objects, but does make it impossible to create temporary objects that do have relationships. </p>
  
  <p>Does anyone know how to account for this change and create/use temporary, working <code>NSManagedObject</code>s? Thanks!</p>
  
  <p>--</p>
  
  <p>Also, check out <a href="https://devforums.apple.com/message/996993#996993" rel="nofollow noreferrer">this</a> relevant post on the iOS 8 forum.</p>