Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 284
shannoga Avatar asked Jul 28 '15 05:07

shannoga


2 Answers

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.

like image 196
Dan Loughney Avatar answered Sep 22 '22 00:09

Dan Loughney


Try using versioning and lightweight migration,

For more over versioning and migration you can check this out.

If above does not help then try it by this way,

Firstly, when you add a core data model in xcode, you actually get a .xcdatamodeld file which is a package like the .xcodeproj file. In finder, you can right click it and "Show Package Contents". You'll see it contains a .xcdatamodel file.

It seems to me you can't rename it fully using xcode. Xcode will change the name of the package file (the momd) as you see it in the UI, but on the file system it has not actually changed.

The nil model error you get is because it can't find the newfilename momd since it still has the old name on disk. (I guess)

I worked around this by creating a new model with the name I wanted, then in the existing model, you can click-drag and select your entities, then just paste them into the new model file. You can then delete the old one and commit all the changes to your SCM.

You can get more help regarding above steps from this link.

like image 20
Akshay Sunderwani Avatar answered Sep 24 '22 00:09

Akshay Sunderwani