Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how should I defensively code for possible core data file corruption or loading issues?

I'm using Core Data in my iPhone application which is fine.

I'm concerned about what approach I should be taking if there is ever an error loading the core data data into my application (or saving it for that matter). In particular a possible situation (not sure what the odds are of this occuring but better to be safe than sorry) where your application can't function at all because it can't load it's data, and restarting the application brings it back to the same please.

Question - what do people typically do if trying to handle this situation? What approach in other words? The only ones that jump to mind so far are:

  1. Build in an error handler that prompts the user re whether they want to delete their data and start again so to speak...I guess this assumes the iPhone will report the problem to the program properly as an error

  2. Suggest to the user they may have to delete the application and reinstall (less work)

  3. Other? Not sure if there are any "clean up" or "repair" code/tips that are possible, or whether one should even think about trying this anyway....

thanks

PS. I haven't got to the point of understanding/dealing with core data migrations for upgrades of the applicaiton where the core data schema has changed. Perhaps this is just another possible situation to which the above questions are still valid...Or not sure whether the approach that one takes within the code (and with user messages) will depend on whether it's a migration error, or a separate error.

like image 867
Greg Avatar asked Apr 24 '11 11:04

Greg


People also ask

How do you code defensively?

The authors recommend developers follow these five defensive programing techniques: design by contract, respect that dead programs tell no lies, implement assertive programming, learn how to balance resources and don't outrun your headlights.

What is defensive programming in VB net?

Defensive programming is an approach to improve software and source code, in terms of: General quality – reducing the number of software bugs and problems. Making the source code comprehensible – the source code should be readable and understandable so it is approved in a code audit.

What is defensive programming in Java?

Defensive programming is an approach to programming that attempts to ensure that software still functions under adverse or unforeseen circumstances.

What is the importance of defensive programming?

It is important for software developers because defensive programming can help them avoid errors in the code, reduce the number of bugs in their programs, and make the programs more secure. This is why it's important for software development.


1 Answers

Better safe than sorry is fine but don't burn a lot of time on this. Core Data corruption is extremely rare. Even repeatedly crashing during saves seldom causes problems.

In any case, the only way to deal with a corrupt store is to delete it. You will probably want to set some kind of flag to delete the file when the app shuts down. Upon detection of the corrupt file, give the user the option to delete the store and restart the app.

like image 66
TechZen Avatar answered Oct 24 '22 14:10

TechZen