Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to handle error when saving managedObjectContext?

What's the appropriate strategy for restoring to the existing persistent store if there's an error when doing a -[NSManagedObjectContext save:] ?

-[NSManagedObjectContext rollback]?

Or should I save a copy of the store on the filesystem before attempting to save: and then, if there's an error, use -[NSPersistentStoreCoordinator setURL:forPersistentStore:] to set the store to the saved store?

Thanks for any pointers.

Gonzalo

like image 1000
Gonzalo Avatar asked Mar 05 '11 02:03

Gonzalo


1 Answers

When the save fails you (as the programmer) did something wrong.

I wrote some pretty complex Core-Data applications and never saw a failing save in production.

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

This means if you have a not-optional relationship don't accept the "Done"-button until the user has selected an entity for this relationship. Or set a default relationship entity.
If you need a NSString in a special format validate that string before accepting it.

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

And now after you made sure that a save never fails you can put [context rollback] in it, because you should never say never.
But before you do this you should ask the user if he wants to send you the error log. Because a save should never fail.

like image 158
Matthias Bauch Avatar answered Sep 26 '22 15:09

Matthias Bauch