Recover from Core Data error in saveContext

In iOS applications that support Core Data, the generated code for the application delegate contains the saveContext function with comments recommending replacing abort() with error recovery code.

Is there any standard boilerplate code (i.e., not application specific) that can really recover from this kind of error?

I was thinking about deleting all the data from Core Data. Will this help? Will it cover any mistake?

Finally, how do I check this recovery code?

+6
source share
1 answer

There is no template code in per se, but usually recommendations indicate that the user has an error message (most likely with UIAlertView ). Usually, saving the context fails when there are invalid objects (with excellent optional attributes), as well as one object that has two versions (this happens when the object is created in one context and is "updated" in another). In the first case, try to show the verification errors and give the user the opportunity to correct them on the same screen on which the save was performed. In the latter case, background merging methods are used, discussed in the CoreData cookie (which uses either deleting one of the object’s versions or merging changes).

Also from my experience, I would say that maintaining context in AppDelegate is not required. I suggest creating a separate context for creating the object and leaving the application delegate only to pass the initial MOC to the rootViewController your window.

+2
source

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


All Articles