How to handle error while saving managedObjectContext?

What is the appropriate strategy for restoring existing persistent storage if there is a runtime error - [NSManagedObjectContext save:]?

- [NSManagedObjectContext rollback]?

Or should I save a copy of the repository in the file system before trying to save: and then, if there is an error, use - [NSPersistentStoreCoordinator setURL: forPersistentStore:] to install the repository in the saved repository?

Thanks for any pointers.

Gonzalo

+6
ios iphone ipad core-data
source share
3 answers

When the failure failed, you (as a programmer) did something wrong.

I wrote some pretty complex Core-Data applications and have never seen a production failure.

Make sure that the values ​​that users can enter can be saved without errors.

This means that if you have an optional relationship, do not accept the “Finish” button until the user selects an object for this relationship. Or set the default relationship object.
If you need NSString in a special format, check this line before accepting it.

This is more user friendly. If you tell him 10 minutes ago you did something wrong, thats why I will delete all your changes , will he think WTF? . If you tell him The value you try to enter is not in the correct format. Please use xxxxx The value you try to enter is not in the correct format. Please use xxxxx , he will think Ok, let change it.

And now, after you have made sure that salvation will never work, you can put [context rollback] , because you should never say never. But before you do this, you must ask the user if he wants to send you an error log. Because salvation should never fail.

+10
source share

I believe, although I do not see this explicitly written in the documents, that if an error occurs during the save, the failure is saved and the data store remains unchanged. This can be easily verified by copying the data store, causing some error, and then comparing the store with the copy.

0
source share

Saving NSManagedContext returns BOOL, YES if it is executed, NO if not. This will tell you for sure whether the data store has actually been changed.

Another thing: Take a look at the “Apple Master Data Programming Guide: Permanent Storage Features” under “Configuring SQLite Store Save Behavior.” They provide a complete example of how to configure complete synchronization settings for the storage to prevent data corruption due to some interruptions during save.

0
source share

All Articles