简体   繁体   中英

Error when saving parent NSManagedObjectContext

I'm using the following MOC setup:

  1. Parent - using persistent store coordinator (Main queue)
  2. Child1 - using parent (Private queue)
  3. Child2 - using parent (Private queue)
  4. Child3 - using parent (Private queue)

Children periodically save their changes and respawn as new snapshots of the main MOC when needed.

All works fine until I try to save the main MOC. This is the error message I get: Cannot update object that was never inserted

Unresolved error Error Domain=NSCocoaErrorDomain Code=134030 "The operation couldn't be completed. (Cocoa error 134030.)" UserInfo=0x1758e200 {NSAffectedObjectsErrorKey=( " (entity: Event; id: 0x1767d3d0 ; data: {\\n dateBegin = nil;\\n dateEnd = nil;\\n identifier = nil;\\n identifierBegin = 0;\\n isProcessed = 1;\\n nPhotos = 0;\\n name = nil;\\n photos = \\"\\";\\n})" ), NSUnderlyingException=Cannot update object that was never inserted.},

It doesn't happen all the time and removing time consuming operations makes it happen less frequently. I also noticed that during the exception the other MOCs are busy saving or querying. I use performBlock or performBlockAndWait for all MOC related operations to run on the right queue.

If relevant, child1 imports base objects, child2 creates events, child3 processes the events and updates both Event and the base object. parent is used to persist tge data to disk and update the UI. Eliminating the thread that uses child3 solves the problem but I'm not convinced it isn't a timing issue.

Any ideas why this happens?

Edit

I think I found the source of the problem. Now looking for solutions.

child3 updated an Event and tried to save the change to parent while child2 decided to delete the Event and already saved this change. To the save is trying to update a non-existing object. Strangely the error ocurs only when I tried to save parent to the PSC.

I've just got the same to the error message and in my case the problem was in storing reference to temporary (not permanent which is generated after saving context) ObjectID, trying to get object from another context (objectWithID:) with id and then performing changes to this object. Save method gave me the same result.

Are you by any chance accessing objects between contexts by referencing to ObjectID?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM