Search code examples
iphonexcodecore-datawatchkittoday-extension

Core Data move data into shared container


I have an already published app that use core data.
Now I want to add support for watch kit and today extension.

I need to move core data into shared container without lose previous user saved data, how can I do that in the best way?


Solution

  • You can migrate a Core Data Stack. A fuller answer can be found here, but the short version is:

    1. Check if the old non-group copy of the data exists
    2. If it does, set up a Core Data stack using that file. Then use migratePersistentStore:toURL:options:withType:error: to move it to the new location. Then remove the old copy.
    3. If the old version doesn't exist, just set up Core Data with the new copy as usual.

    (The problem with Stephen's answer is that it assumes that the Core Data stack is a single SQLite file, which isn't always true.)