开发者

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 a开发者_运维技巧bort() 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜