Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recovering from a Core Data error in saveContext

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

Is there some standard boilerplate (i.e. not application-specific) code that could actually recover from this kind of error?

I was thinking of erasing all data from Core Data. Would that help? Would it cover against any kind of error?

Lastly, how do I test this recovery code?

like image 223
Ovesh Avatar asked Sep 16 '11 05:09

Ovesh


1 Answers

There is no boilerplate code per-se, but usually guidelines suggest presenting user with an error message (most probably with UIAlertView). Usually context saving fails when there are invalid objects (with unset non-optional attributes) as well as one object having two versions (this happens when object is created in one context and "refreshed" in another). The former case begs to show the validation errors and giving user a chance to fix them in the same screen where saving was performed. The latter case has background merging techniques discussed in CoreData cookbook (which employs either dropping one of the versions of the object or merging changes).

Also from my experience I'd say that context saving in AppDelegate is not needed much. I suggest creating separate context for object creation and leave app delegate only to pass initial MOC to rootViewController of your window.

like image 169
Eimantas Avatar answered Oct 17 '22 16:10

Eimantas