0

My iOS app is using NSCoreData and stores lots of data in the database. I designed the database schema in *.xcdatamodeld file in xcode. I am planning to update my application soon and need to change the schema a little. However, when I tested it, new version is removing all old data, saved in previous version.

How can I effectively migrate to new database schema without loosing old data in the app? I tried to google for a suitable solution, but could not find any effective idea, how to do it.

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
Pixelord
  • 621
  • 9
  • 24
  • It would be helpful to know what extent you're changing your schema. If they are minor changes, an automatic migration will take care of it - you just need to add another model version number. If they are significant changes, then you'll need to write a manual migration – LyricalPanda Aug 22 '14 at 14:31
  • Hi, The changes are minor. I needed to add some extra attributes in an entity. Could you please point me any reference, how to set version number in my model? – Pixelord Aug 22 '14 at 15:03
  • 1
    Then you just need to do an automatic migration. Search for it as it's pretty easy to implement in your code. Also a quick google search for "xCode model number" points you to the Apple Docs: https://developer.apple.com/library/Mac/recipes/xcode_help-core_data_modeling_tool/Articles/creating_new_version.html#//apple_ref/doc/uid/TP40010379-CH8-SW1 and https://developer.apple.com/library/Mac/recipes/xcode_help-core_data_modeling_tool/Articles/setting_current_version.html#//apple_ref/doc/uid/TP40010379-CH9-SW1 – LyricalPanda Aug 22 '14 at 15:16
  • Thanks! It was very helpful. One small question though. If my already published app doesn't have any model version number, is it going to work if I assign a version number in the new one? – Pixelord Aug 22 '14 at 16:21
  • It should. But honestly this question is answered extremely quickly for you - install the app from the app store, then run it off xCode with the new model number on a device. You can see what happens for yourself. If it doesn't work, then you can research on how to fix it. – LyricalPanda Aug 22 '14 at 16:28
  • What about NSCoreData? – Lorenzo B Aug 24 '14 at 14:14

1 Answers1

0

Use documents directory it will keep your data even if you update the app.

you can find out about documents directory here What is the documents directory (NSDocumentDirectory)?

davut dev
  • 228
  • 1
  • 2
  • 11