Search code examples
iosobjective-ccore-datacore-data-migration

Core Data Migration: Possible to delete old store except for a few specific objects?


The new version of our app is a complete redo, different in almost every way. Reading about migration, I think I'd definitely fall into the heavy camp (added relationships), but I'm not sure I actually need most of the data and not sure I have the chops for a complex migration. That being said, I realized users can save favorite stories and I'd like to preserve some of that data (but not whole NSMO as a story entity is completely different now). But that's all I need, just a few pieces of data from a few NSMOs.

I've got the detection setup with -isConfiguration:compatibleWithStoreMetadata: and have deleted the old store completely with -removeItemAtURL: as a wort case scenario, but is there any way to fetch a couple things before I delete and build new objects?


Solution

  • Yes, you can stand up the old store with the old model and then fetch data out of it and manually insert it into the new stack. Then delete the old store afterwards. The actual workflow would be:

    • New model is a completely separate file
    • New store is a completely separate file (new file name)

    Then:

    1. Look for old file name on disk. If it doesn't exist just stand up the stack
    2. Stand up old stack.
    3. Stand up new stack.
    4. Copy data from old stack to new stack.
    5. Save new stack.
    6. Release old stack and delete old store file from disk.

    No migration needed.

    i added old model file. then in the psc method, i detect if compatible and if not, stand up old stack, initiating model and psc both pointing to old one i just added and then creating context. added old class files for entity i need and fetched just the favorites and it worked! i know i can create new objects and insert to new stack, but what's the best way to save the new stack and release old when done? i still seem to be able to fetch even after removeitematurl. and is app delegate best place for all this?

    First, the AppDelegate is NOT the place for this. Create a DataController that subclasses NSObject. Put all your Core Data code there and then pass that DataController around.

    Next, you are not looking for a migration state. You are looking for files on disk with NSFileManager. If the old file exists then stand up the old store and the new store, copy data over. Then remove the old file.

    To release the old stack, just set the references (MOM, PSC and MOC) to nil. ARC will remove them from memory.