There is another way to provoke "CoreData: error: Mutation of a managed entity ... after it has been removed from the context." message.
- If you have a multi-layer kernel data stack
- You keep the link to the managed entity in the foreground
- Background thread updates the same main data object
When an update bubbles up to the foreground stream, it will invalidate your object. You need to discover this and get a new version.
Hold the weak link in the cache object. Then write a getter that checks this cache object on nil and retrieves a new version of the object.
weak var cachedObject: NSManagedObject? var object: NSManagedObject { get { objc_sync_enter( self ) defer { objc_sync_exit( self) } guard nil == cachedObject else { return cachedObject! } guard let object =
Or you can just retrieve the object every time in getter.
source share