Mutation of a managed object after its removal from the context

I have two classes in my underlying data model: Sample and Photo , with a 1: N ratio. I use the only NSManagedObjectContext in my application, which is used as a notepad for managed objects. Therefore, I always insert a newly created object into the context. If users decide to abandon their changes, I just roll back from the context. Otherwise, the context is preserved.

Error

CoreData: error: Mutation of the managed object 0x1704253a0 (0x1702a1560) after its removal from the context.

prints to the console after the Photo object is removed from the context. Deletion occurs before the context is saved, so Photo objectID is temporary. Here is a quick code:

 function addPhoto(to sample: Sample) -> 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) 

The strange thing is that there is no error or exception, and my code completes to the end. However, the error message is displayed on the console.

I believe this happens because the context invalidates all Photo properties before deleting it. I know that the properties are revoked because I added observers to them.

How to prevent this error from appearing in the console?

Thanks.

+6
source share

Source: https://habr.com/ru/post/1014196/


All Articles