views:

336

answers:

2

Hello,

I have an application that allows you to edit some percentages, however it will only let you commit those changes if the percentages add up to 100. However because the Core Data template includes the save code in the application will terminate. If the user changed something and then exited the application, the item would be of course saved even though it did not add to a 100%.

Therefore I simply decided to comment out the save in the application will terminate. I know the other option would be to use another context for the edit and then merge the changes or setting my context values until the actual save point. However I do not see any harm in commenting out this line, since I save whatever I want in my application when the user clicks the save button, so my question is: is the save on the application will terminate mandatory? what possible consequences could this have?. It is important to note that the application continues to work just fine after commenting this lines (which is what I expected).

Thank you in advance.

-Oscar

A: 

Its not mandatory to save on application will terminate. You can save when ever you feel appropriate for the context of the app.

Benjamin Ortuzar
+1  A: 

You can save whenever you like.

However, you will never know when the app will terminate. Unlike applications on more conventional platforms e.g desktops, the iPhoneOS will terminate your app (from the apps perspective) at random. The only warning you will get will be the applicationWillTerminate message sent to the app delegate. If you don't handle saves there then it is highly likely that at some point, your users will lose data.

I think you should reconsider your design. It sounds like you're putting calculation into the managedobjects that could (1) be handled elsewhere in code or (2) be handled by transient properties. You shouldn't have a condition in which the managedobject can't be saved at the drop of hat. Doing so makes your datamodel utterly dependent on external code for its internal integrity. This causes problem with maintenance, portability and upgrading.

TechZen
If the data is not valid until it adds up to 100%, not saving the intermediate state may be completely appropriate to the application design.
Tim
Thank you for your answer. Yes, I will redesign so that the managedobject is only modified when the data adds up to 100%.
OscarMk
I decided to register an undomanager for those changes and then simply in the application will terminate do this:while ([managedObjectContext.undoManager canUndo]) { [managedObjectContext.undoManager undo]; }And then of course if the data adds to a 100% the undomanager is set to nil.and then save. It works good, would this be a good approach?
OscarMk
Sounds good but then again I don't know the details of your app.
TechZen