12-11-2011 05:02 AM
Struggling for a while with the fallowing problem:
I have a pretty complex data structure (a tree) that contains data about a file structure and some additional info. I am persisting this. (Implementing Persistable and all is fine).
If I load my structure from the PersistenceStore, modify it (say add or removes branches) and leave the app without saving (calling commit) I can still see the modification I have made.
Is this normal behaviour?
One solution I can think of is making a copy of the loaded structure and use that in my code. Then the copy will go to persistence only if I hit save, is there another way?
Solved! Go to Solution.
12-11-2011 06:46 AM
The result you see is correct and expected. However if you restart the phone, these updates will be lost.
If you wish to make temporary updates to a persisted Object you do need to clone it.
Of course cloning it can cause issues.
Say you have a shared object, that is updated via the UI and an alternative mechanism, say using the Push API. When you clone this and give this to the user, it contains certain values. However let us say that the Push API, while the user is updating the cloned Object, also updates the Object. Now when the user saves, they will replace the recently updated (via Push API) Object with the old data.
One approach is to have an update time on the Object. All methods of updating the Object must set this to the current time. So when your user Ui goes to update the shared Object, prior t the update it checks the persisted store object's timestamp and makes sure it is the same as the one it got when the Object was cloned. If not, the user update is thrown away.
Alternatively you could also lock the Object in some way, so the Push API has to wait for the User to finish before doing its update. I would use some locking mechanism rather than "synchronized" to do this.
12-11-2011 07:29 AM
Thanks a lot for your help.
Fortunately I will not have 2 sources of updates for my object. I only have the UI.
So in order to have a save/discard mechanism for my updates I should clone the object after loading it from PS and do the modifications on the clone...