Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to recover from optimistic locking failure

I'm deleting all objects from my Model, except certain objects which the user has interacted with.

After deleting I fetch those saved objects and update their information, from a JSON, and when I save the managedContext the app crashes printing 'Unable to recover from optimistic locking failure.'

I've been looking but I haven`t found any information on what this message means, any idea?

Thanks.

like image 491
subharb Avatar asked Nov 09 '22 19:11

subharb


1 Answers

Google first result, documentation from Apple. (Where exactly have you been "looking"?)

Problem: You see the error message, "Could not merge changes".

Cause: Two different managed object contexts tried to change the same data. This is also known as an optimistic locking failure.

Remedy: Either set a merge policy on the context, or manually (programmatically) resolve the failure. You can retrieve the currently committed values for an object using committedValuesForKeys:, and you can re-fault the object (so that when it is next accessed its data values are retrieved from its persistent store) using refreshObject:mergeChanges:.

Even though the error message is different, this sounds very much like your case, for you most likely have multiple contexts that try to modify the same objects.
Try setting the merge policy to NSMergeByPropertyObjectTrumpMergePolicyType.

like image 150
Mundi Avatar answered Nov 14 '22 22:11

Mundi