title: Core Data cannot delete object that was never inserted error
body: <p>Disclaimer: I am a total CoreData noob. I am running my app on an iPhone 4S with iOS 6.0.1.</p>
  
  <h2>Background</h2>
  
  <p>A part of my app acquires images using a <code>UIImagePicker</code> and saves them to disk using CoreData. Said images are associated with Activities in the model. Activities have a one-to-many relationship with Images.</p>
  
  <p>I need to save images to disk immediately after capture and then fault them out of memory, retaining a thumbnail for display. If I do not, iOS will kill my app for using too much memory after a relatively small number of images have been captured.</p>
  
  <p>I made Thumbnails a separate entity to avoid needing to bring the entire image into memory in situations when all I need is the thumbnail. Thumbnails have a one-to-one relationship with Images. Also, for various reasons I am not using Core Data to store the thumbnail image data; instead, I store the filename, and manage the files on disk myself.</p>
  
  <p>All of this means that, should the user cancel adding an Activity after they have already added at least one Image, then both the Images and the Thumbnails that the user added need to be deleted.</p>
  
  <h2>Problem</h2>
  
  <p>When I try to delete the added image objects, two things happen. First, Core Data complains about my delete rules:</p>
  
  <pre><code>Core Data: annotation: repairing missing delete propagation for to-one relationship image on object &lt;Thumbnail: 0x1dd4d9e0&gt; (entity: Thumbnail; id: 0x1dd4d410 &lt;x-coredata:///Thumbnail/tE7E99D89-C986-4287-ABBD-C6BA7006E9264&gt; ; data: {
      activity = nil;
      fileName = "ECD70C72-745A-4959-BF41-586E9521F1D6";
      image = "0x1dd9f960 &lt;x-coredata:///Image/tE7E99D89-C986-4287-ABBD-C6BA7006E9263&gt;";
  }) with bad fault 0x1dd9f960 &lt;x-coredata:///Image/tE7E99D89-C986-4287-ABBD-C6BA7006E9263&gt;
  </code></pre>
  
  <p>Second, when I try to save the managed object context, the save fails, and Core Data throws an exception. Here's the output from the NSError object:</p>
  
  <pre><code>Error Domain=NSCocoaErrorDomain Code=134030 "The operation couldn’t be completed. (Cocoa error 134030.)"
   UserInfo=0x1f0ca270 {NSAffectedObjectsErrorKey=(
      "&lt;Image: 0x1dd9f910&gt; (entity: Image; id: 0x1dd9f960 &lt;x-coredata:///Image/tE7E99D89-C986-4287-ABBD-C6BA7006E9263&gt; ;
           data: {
               activity = nil;
               image = nil;
               thumbnail = nil;
           })"
  ), NSUnderlyingException=Cannot delete object that was never inserted.}
  </code></pre>
  
  <p>SO. Apparently I'm trying to delete an object that I never inserted? Only I'm pretty sure I did.</p>
  
  <h2>Relationship Details</h2>
  
  <p>Activity &lt;1-*> Image; delete rule: cascade<br>
  --> Inverse: Image &lt;1-1> Activity; delete rule: nullify</p>
  
  <p>Image &lt;1-1> Thumbnail; delete rule: cascade<br>
  --> Inverse: Thumbnail &lt;1-1> Image; delete rule: nullify</p>
  
  <h2>Relevant Code</h2>
  
  <p>Image acquisition:</p>
  
  <pre><code>- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
  {
      UIImage* acquiredImage = (UIImage*)info[UIImagePickerControllerEditedImage];
      if (!acquiredImage)  {
          acquiredImage = (UIImage*)info[UIImagePickerControllerOriginalImage];
      }
  
      // Save the image to disk so we can fault it and free up its memory
      NSManagedObjectContext* moc = [(ATAppDelegate*)[[UIApplication sharedApplication] delegate] managedObjectContext];
      Image* imageToSave = [NSEntityDescription insertNewObjectForEntityForName:@"Image" inManagedObjectContext:moc];
      Thumbnail* thumbToSave = [NSEntityDescription insertNewObjectForEntityForName:@"Thumbnail" inManagedObjectContext:moc];
      [addedImageManagedObjects addObject:imageToSave];
      [addedThumbnailManagedObjects addObject:thumbToSave];
  
      [thumbToSave setThumbnailImage:acquiredImage];
      imageToSave.thumbnail = thumbToSave;
  
      NSError* error;
      [moc save:&amp;error];
      // TODO: error handling
  
      // I know it doesn't look it, but this call will turn the imageToSave into a fault, thus freeing up its memory
      [moc refreshObject:imageToSave mergeChanges:NO];
  
      // NOW, we can add the thumbnail image to our collection view
      // NOTE, -thumbnailImage is a method on my Thumbnail NSManagedObject subclass
      [self addImage:[thumbToSave thumbnailImage] toCollectionView:imageDisplayView];
  
      [self dismissModalViewControllerAnimated:YES];
  }
  </code></pre>
  
  <p>Cancel handler:</p>
  
  <pre><code>- (IBAction)cancel:(id)sender {
      NSManagedObjectContext* moc = [(ATAppDelegate*)[[UIApplication sharedApplication] delegate] managedObjectContext];
  
      // Delete any images we saved to disk
      // Note that the activity object's changes get discarded b/c they were made on the child managed object context
      // (We couldn't do that for images b/c we needed them to be written to disk and then faulted out of memory)
      for (Thumbnail* thumb in addedThumbnailManagedObjects)  {
          // This method deletes the thumbnail image file whose storage I am managing myself
          [thumb deleteThumbnailFile];
          // I shouldn't have to explicitly delete the thumbnail since the delete of its image should cascade down...right?
      }
      for (Image* image in addedImageManagedObjects)  {
          [moc deleteObject:image];
      }
  
      NSError* error;
      if (![moc save:&amp;error])  {
          NSLog(@"%@", error);
      }
      // TODO: error handling
  
      [self resetToPristineState];
      [self.presentingViewController dismissModalViewControllerAnimated:YES];
  }
  </code></pre>
  
  <h2>Summary</h2>
  
  <p>I can't see where I'm going wrong, and the error messages I'm getting aren't helpful. Worse, googling the error messages isn't giving me any useful results. Hence, I turn to you wonderful experienced people for help.</p>