2

I'm using NSCoding to encode my objects and save them to disk as a "caching" feature not having to download data every time my app is started. Right now I'm saving this data in the Documents folder of the app which I have read is not deleted when updating the app.

So my concern is that I do some update to my class like adding an instance variable. And then uploads the app to the App Store. So when the user updates to new version the old objects that are saved in the Documents folder are encoded without that new instance variable that I had added in the new version. So that when my app will try to decode the saved objects during startup it will fail because the "old" objects from the Documents folder were not encoded with this new variable?

How would I deal with this problem? Make sure I write my classes "right" from the start? I'm sure I will eventually need to modify one of my classes and then break the old saved objects on disk.

Peter Warbo
  • 11,136
  • 14
  • 98
  • 193

2 Answers2

2

Change either the filename or encoding key of the objects when you decide on a new version.

For example if you are now saving your objects in an collection to 'myObjectsFile', when you have a new version use the filename 'myObjectsFile2'. When your application launches check for 'myObjectsFile' if it is there load your old objects and migrate them to the new object version, then save those migrated objects to 'myObjectFile2' and delete 'myObjectFile'.

On the next launch you are all set, since you have deleted 'myObjectsFile'.

NJones
  • 27,139
  • 8
  • 70
  • 88
0

YOu can version your objects by having a version property that you would guarantee to always be there.

After loading an object from disk, don't do anything except check the version property. (You could also check for the existence of the version property first.) If your current code base does not support the version of your object, simply discard it.

Brad The App Guy
  • 16,255
  • 2
  • 41
  • 60