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

557 Views Asked by At

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.

1

There are 1 best solutions below

2
On BEST ANSWER

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.