title: Mutating a managed object after it has been removed from its context
body: <p>I have two classes in my core data model: <code>Sample</code> and <code>Photo</code>, with 1:N relationship.
  I use a single <code>NSManagedObjectContext</code> in my app which is used as a scratch pad for managed objects. Hence, I always insert a recently created object in the context. If the users chooses to discard her changes, I simply rollback the context. Otherwise, the context is saved.</p>
  
  <p>The error </p>
  
  <blockquote>
    <p>CoreData: error: Mutating a managed object 0x1704253a0  (0x1702a1560) after it has been removed from its context. </p>
  </blockquote>
  
  <p>is printed in the console after after the <code>Photo</code> object is deleted from its context. The deletion happens before the context is saved, so the <code>Photo</code> <code>objectID</code> is temporary. Here is the swift code:</p>
  
  <pre><code>function addPhoto(to sample: Sample) -&gt; Photo {
      let photo = Photo(context: managedObjectContext)
  
      sample.addToPhotos(photo)
      photo.sample = sample
  
      return photo    
  }
  
  function remove(photo: Photo) {
      photo.sample.removeFromPhotos(photo)
      photo.sample = nil
  
      managedObjectContext.delete(photo)
  }
  
  //user adds new Photo
  let sample = loadSample() //sample is loaded from database
  let photo = addPhoto(to: sample)
  
  //later on user decides to remove the added Photo before the context is saved
  remove(photo: photo)
  </code></pre>
  
  <p>The strange thing is that there is no error or exception thrown, and my code completes to the end. However, the error message is printed on the console.</p>
  
  <p>I believe this happens because the context nullifies all <code>Photo</code> properties before actually deleting it. I know the properties are nullified because I added observers to them.</p>
  
  <p>How do I prevent this error from showing up in the console?</p>
  
  <p>Thank you.</p>