Search code examples
ioscore-datansuserdefaultsdata-migrationios-app-group

Moving data to shared group in already launched app


My app is already on the App Store, but now I'm creating an Apple Watch Extension which also means storing data in a Shared Group.

How do I go about moving the data from where it's currently stored (Core Data & UserDefaults) for the customers that have already downloaded my app, the first time they launch the new update?

What's the most efficient and least resource intensive way of doing this, and only doing it for old users the first time they open the new update?


Solution

  • I had a similar issues, only with user defaults though. All I did was that when the app launches it looks in the shared group location first and if it fails it looks in the old location and then moves it to the new location.

    That means that it only has to check multiple locations the first time, something like this process:

    • create UserDefaults with shared suite name
    • if there are no keys with values in the shared suite that you require it's probably the first time the user has launched since migration is required
    • so now get all the values from the standard UserDefaults and put them into the UserDefaults you created with the shared suite name

    Next time you launch the app it will find the keys/values in the shared suite and not need to look at the standard UserDefaults at all.