Search code examples
iosxcodecore-datacore-data-migration

Renaming coredata .xcdatamodeld file and migration


We had a 2 separated apps that we have merged to 2 targets in the same workspace. Both had their core data model (app1.xcdatamodeld and app2.xcdatamodeld).

Now that both are in the same workspace there is no need for 2 models as they are 99% similar. So I want to change the name of the .xcdatamodeld package to allApps.xcdatamodeld. I was able to rename it but I do not find any reference about the behaviour of migration when doing so.

This is the error -

2015-07-28 09:04:06.079 AppName[51306:1423128] CoreData: error:
-addPersistentStoreWithType:SQLite configuration:(null) URL:file:///Users/shani/Library/Developer/CoreSimulator/Devices/EA75A69F-B108-4036-AB3D-B923F83D16DE/data/Containers/Data/Application/B1CB3AF3-DEF4-40DF-9A61-721063ECBB1D/Documents/AppName.sqlite
options:{
    NSInferMappingModelAutomaticallyOption = 1;
    NSMigratePersistentStoresAutomaticallyOption = 1;
    NSSQLitePragmasOption =     {
        "journal_mode" = WAL;
    }; } ... returned error Error Domain=NSCocoaErrorDomain Code=134130 "The operation couldn’t be completed. (Cocoa error
134130.)" UserInfo=0x7fc39c194360 {URL=file:///Users/shani/Library/Developer/CoreSimulator/Devices/EA75A69F-B108-4036-AB3D-B923F83D16DE/data/Containers/Data/Application/B1CB3AF3-DEF4-40DF-9A61-721063ECBB1D/Documents/AppName.sqlite,
metadata={
    NSPersistenceFrameworkVersion = 519;
    NSStoreModelVersionHashes =     {
        Attempt = <b4d1e878 69a97917 e751a67c 89e1887a ae4df1dc f380e4ee c9585f60 f39ff51e>;
        BookmarkFlashcard = <33112f7c 2fa20bec eb2e019c b5d619b4 92be2547 a65fabee 1ac86ab7 92dd77bb>;
        BookmarkQuestion = <38634b49 71f0d43c 6f9fbba9 bc2f1b81 675c9b3c f2da017e fc8de9d8 4e18a4a0>;
        Category = <b0e4d91b 0c0ebc71 ed0f403c 9a0eec20 fc1686aa 0169bc60 4bcc7e57 165bce15>;
    };
    NSStoreModelVersionHashesVersion = 3;
    NSStoreModelVersionIdentifiers =     (
        ""
    );
    NSStoreType = SQLite;
    NSStoreUUID = "F1B37CD0-8B89-4893-A262-1013C3544026";
    "_NSAutoVacuumLevel" = 2; }, reason=Can't find model for source store} with userInfo dictionary {
    URL = "file:///Users/shani/Library/Developer/CoreSimulator/Devices/EA75A69F-B108-4036-AB3D-B923F83D16DE/data/Containers/Data/Application/B1CB3AF3-DEF4-40DF-9A61-721063ECBB1D/Documents/AppName.sqlite";
    metadata =     {
        NSPersistenceFrameworkVersion = 519;
        NSStoreModelVersionHashes =         {
            Attempt = <b4d1e878 69a97917 e751a67c 89e1887a ae4df1dc f380e4ee c9585f60 f39ff51e>;
            BookmarkFlashcard = <33112f7c 2fa20bec eb2e019c b5d619b4 92be2547 a65fabee 1ac86ab7 92dd77bb>;
            BookmarkQuestion = <38634b49 71f0d43c 6f9fbba9 bc2f1b81 675c9b3c f2da017e fc8de9d8 4e18a4a0>;
            Category = <b0e4d91b 0c0ebc71 ed0f403c 9a0eec20 fc1686aa 0169bc60 4bcc7e57 165bce15>;
        };
        NSStoreModelVersionHashesVersion = 3;
        NSStoreModelVersionIdentifiers =         (
            ""
        );
        NSStoreType = SQLite;
        NSStoreUUID = "F1B37CD0-8B89-4893-A262-1013C3544026";
        "_NSAutoVacuumLevel" = 2;
    };
    reason = "Can't find model for source store"; } 2015-07-28 09:04:06.089 AppName[51306:1423128] CoreData: annotation:
NSPersistentStoreCoordinator's current model hashes are {
    Attempt = <b4d1e878 69a97917 e751a67c 89e1887a ae4df1dc f380e4ee c9585f60 f39ff51e>;
    BookmarkFlashcard = <33112f7c 2fa20bec eb2e019c b5d619b4 92be2547 a65fabee 1ac86ab7 92dd77bb>;
    BookmarkQuestion = <38634b49 71f0d43c 6f9fbba9 bc2f1b81 675c9b3c f2da017e fc8de9d8 4e18a4a0>;
    Category = <b0e4d91b 0c0ebc71 ed0f403c 9a0eec20 fc1686aa 0169bc60 4bcc7e57 165bce15>; }

Is there a way to rename name1.xdatamodel to name2.xdatamodel in an app that is already in production ?

Thanks


Solution

  • Rather than renaming the app1.xcdatamodeld or the app2.xcdatamodeld you should create a new one (allApps.xcdatamodeld). This new one model can be just a copy of whichever model you want to keep.

    Then in each of the two targets add an .xcmappingmodel file and link it to the allApps.xcdatamodeld and perform any translations needed.

    The way coreData works, you'll want to keep the original app1 and app2 xcdatamodeld files around to ensure that you never break a user who happens to have your very oldest app still running on their device.

    File > New > File > Core Data > Mapping Model. Choose the source model and click Next. enter image description here

    Choose the Target data model and click Next. enter image description here

    Do this again in your other target for app2 and you should be OK.