James Parker
James Parker

Reputation: 2242

Magical Record Custom Core Data Migration not pulling existing data

I'm trying to perform a custom migration on a Core Data Model, but the only data that will migrate is the data associated with the custom migration.

In the previous version of my application I was using

[MagicalRecord setupCoreDataStackWithStoreNamed:@"xxxxxx.sqlite"];

To enable automatic migrations I've switched to use.

[MagicalRecord setupCoreDataStackWithAutoMigratingSqliteStoreNamed:@"MTDataStore.sqlite"];

In the new application version I've also created a new model version, a mapping model and a custom transformation policy to use for the migration. The pre-existing version has a total of 4 models.

The changes to the model include an added attribute to one entity, which should be handled by the automatic migration settings. The other change is a removal of 4 attributes (attr1,attr2,attr3,attr4) from an entity and an addition of one attribute (key) to that entity. I'm then mapping the old attributes to the new attribute 'key' which is a string.

This is the value expression I'm setting for the 'key' attribute that the transformation policy is targeting.

FUNCTION($entityPolicy, "keyToJSONString:" , $source)

And this is the code within the transformation policy.

-(NSString*)keyToJSONString:(MTWirelessCDKey*)wirelessCDKey
{
    NSString *lengthJSON = [MTUtility JSONString:wirelessCDKey.length];
    NSString * jsonKey = [NSString stringWithFormat:@"{\"gauge\":\"%@\",\"length\":\"%@\",\"diam\":\"%@\",\"head\":\"%@\"}",
                          wirelessCDKey.attr1,
                          lengthJSON,
                          wirelessCDKey.attr2,
                          wirelessCDKey.attr3];
    return jsonKey;
}

When I run the new version of the application using Xcode, installing over the old version, the model that has the mapping model and custom transformation policy shows up with the correct data in my sqlite database, but all of the data associated with the other three models is missing.

Just for troubleshooting purposes I tried to create a mapping model for a different model, which from my understanding is unnecessary. When I run the new app the data for that model then comes over, but not the previous model that was working.

In the new version of the application I also set the model that I was running the migration on back to the original, just to make sure the other data came through fine and it then pulls over all the old data.

Any suggestions would be appreciated.

Note: This is all being done in Objective-c in Xcode 7 (iOS 9 SDK) on a device.

Update: So I've spent the last 24hrs or so really attacking this problem and realized that I didn't have a good understanding of what was actually happening when I started this question. So I'm going to explain it better with a solution now.

When I initially create the project I setup 4 different xcdatamodeld references. All of which ended up being stored in the same sqlite store. Now based on what I've read creating multiple xcdatamodelds isn't necessarily a bad thing, but you need to make sure the you store them each in there own sqlite store.

So, the solution I found / came up with is actually pretty simple. Since I was storing all of the data in the same sqlite store I was able to merge all of my xcdatamodeld objects into one.

Say you have 4 xcdatamodeld objects. xcdatamodeld1,2,3 and 4.

You can take xcdatamodeld1,2 and 3 and merge them manually into xcdatamodeld4 by opening the model objects located in the finder and merging the xml all into one file. When you startup your core data store next time it will seem as if nothing has changed.

After that you can run migrations as you normally would.

Upvotes: 1

Views: 435

Answers (1)

James Parker
James Parker

Reputation: 2242

The problem was that there were more then one xcdatamodeld objects in the Xcode project that were all being stored to the same sqlite store.

To fix the issue I manually merged all of the xcdatamodeld objects in one model object by opening the model objects in the finder, opening the contents folder in a text editor, copying the xml entity objects and merging the elements items to the designated model.

Then delete the old xcdatamodeld objects that are now contained within the merged xcdatamodeld.

Once you have, lets say a normalized, xcdatamodeld object you can run migrations as you normally would.

Upvotes: 1

Related Questions