title: ios - Core data - app crash if deleting before save finished
body: <p>My app simply add some users informations (name, birthdate, thumbnail, ...) with Core Data.</p>
  
  <p>I noticed that if I delete a user right after created it, my app just stop working (not a crash, xCode returns no crash log, nothing).</p>
  
  <p>I'm using asynchronous nested context for saving my users informations so I guess that behavior is due to the fact that my <code>delete</code> statement is executing before my <code>save</code> statement.</p>
  
  <p>But since i'm a total beginner with Core Data, i don't really know how to handle that. I don't even know if i declared nested contexts the right way.</p>
  
  <p>Here's my <code>save</code> codes :</p>
  
  <pre><code>NSManagedObjectContext *tmpContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
      tmpContext.parentContext = self.backgroundManagedObjectContext;
  
      BSStudent *newStudent = (BSStudent *)[NSEntityDescription insertNewObjectForEntityForName:kBSStudent inManagedObjectContext:tmpContext];
  
      newStudent.firstname = firstname;
      newStudent.lastname = lastname;
      newStudent.birthdate = birthdate;
      newStudent.thumbnail = thumbnail;
      newStudent.createdAt = [NSDate date];
  
      [self dismissViewControllerAnimated:YES completion:nil];
  
      [tmpContext performBlock:^{
          [tmpContext save:nil];
  
          [self.backgroundManagedObjectContext performBlock:^{
              NSError *error;
              if (![self.backgroundManagedObjectContext save:&amp;error]) {
                  NSLog(@"%@", [error localizedDescription]);
              }
  
              [self.managedObjectContext performBlock:^{
                  NSError *error;
                  if (![self.managedObjectContext save:&amp;error]) {
                      NSLog(@"%@", [error localizedDescription]);
                  }
              }];
          }];
      }];
  </code></pre>
  
  <p>For precision, <code>self.managedObjectContext</code> is a <code>NSPrivateQueueConcurrencyType</code> and <code>self.backgroundManagedObjectContext</code> is a <code>NSMainQueueConcurrencyType</code>. And <code>self.backgroundManagedObject</code> is a child of <code>self.managedObjectContext</code>.</p>
  
  <p>Here's my delete codes :</p>
  
  <pre><code>    BSStudent *student = objc_getAssociatedObject(alertView, kDeleteStudentAlertAssociatedKey);
  
      // on supprimer l'objet et on sauvegarde le contexte
      [self.managedObjectContext deleteObject:student];
      NSError *error;
      if(![self.managedObjectContext save:&amp;error]) {
          NSLog(@"%@", [error localizedDescription]);
      }
  </code></pre>
  
  <p>Can someone know how to handle this situation properly ?</p>