Move a Core Data entity and its data into new Core Data model file through Migration - ios

Is it possible to move a Core Data entity Car and its data in Model1 into a new Core Data model Model2 using migration? Model2 will also have a new CarOwner entity + other new relationships (so model file is different than Model1). Is this possible using lightweight migration or do I have to use custom migration? I'm using Magical Record to setup my Core Data Stack.
I have been using lightweight migration for years with success. So I don't have any custom migration mechanism in place. I want to ask first before I implement a new system so I can incorporate CD custom migration into my existing MagicalRecord Core Data stack.
EDIT: updated question to clarify that Model1 and Model2 have differences.

With the same xcdatamodel file and different versions you can use a Mapping Model file, but being different mom files i guess those are different stacks and migration will not work.

Model migration is only relevant if the model has changed-- meaning, if the entities contained in the data model don't match the entities saved in the persistent store file. Migration doesn't depend on what model file you use, it depends on the entity hashes contained in that model.
Meaning: If your new model file has exactly the same entities as the persistent store file has, you don't actually need to migrate anything. Just start using the new model file. However keep in mind that if you ditch the old model file and all of its old versions, you won't be able to migrate from older versions of that model any more.

Related

Add new entity in already migrated core data

i have a core data which has been migrated once before to add an entity. Now i am trying to add another new entity that will have relationship with previous entities, my question is that do i need to create a third model to add this new entity or i can use the second one?
If you have not released the second core data model beyond your development environment, then you can just modify the second model to include your new entity and its relationship. If you have released the second model in an app, then you will need to create a third model.

Core data versioning/migration after deleting entities

I have a database which consists of 3 entities, say
'IronMan', 'CaptainAmerica' and 'SpiderMan'.
With new changes, I want to delete all three entities and create another entity called 'Thanos'.
I would not need to use any of the code and data stored earlier with entities 'IronMan', 'CaptainAmerica' and 'SpiderMan'. Do I need to do core data versioning or migration in this case?
As mentioned in Apple's documentation
https://developer.apple.com/documentation/coredata/using_lightweight_migration
You can add, remove, and rename entities in the hierarchy.
So, yes, you need to provide migration (new version model) but can stick to a light-weight migration, hence it will be done automatically based on changes done between the 2 models.

How to actually execute CoreData heavyweight migration

I am performing a CoreData heavyweight migration in my app. I have created a mapping model, made associated NSEntityMigrationPolicy subclasses, and selected the new model as the current. However, the migration does not appear to be happening. What do I need to do for this to actually occur?
If your model is not eligible for lightweight migration, then you have to do it manually. Simply this process can explained as reading records from old store and dumping them in the new store according to new model.
See here and here for more details
You have to manually switch off the automatic migrations from persistent store coordinator so core data would not automatically infer mapping model and use your custom created model in core data stack.
key: NSInferMappingModelAutomaticallyOption
value: false

iOS - How to migrate 2 Entities into 1 using Core Data?

In my current model version I have this 4 Entities :
Satellite (with relationship to one to SatelliteAnimation)
SatelliteAnimation
Radar (with relationship to one to RadarAnimation)
RadarAnimation
In my App I have different blocs in which I display information.
This is why I have entities RadarAnimation and SatelliteAnimation, in order to recognize those blocs by type.
Anyway, my issue is that I want to migrate 2 blocs in 1.
And instead of having 4 Entities, I would like to have :
Radar
Satellite
Animations
I don't need the relationships anymore.
I know I can rename an Entity in a lightweight migration process.
What I would like to do is :
Create a new data model version
Delete the SatelliteAnimation entity
Delete the relationship between Satellite and SatelliteAnimation
Delete the relationship between Radar and RadarAnimation
Rename the entity RadarAnimation by Animations
Set the new data model version as current model version
Does this way of handling my issue will affect the migration process ?
Can I rename and remove relationships in a new data model version ?
This should be possible with automatic lightweight migration. Deleting entities and relationships just works with no extra steps, so that's fine.
To rename something you'll use the renaming identifier in the model editor. After you create the new version of your model, select the entity you want to rename in the model editor. Change its name but set the renaming identifier to the old name. In your case, change the entity name from RadarAnimation to Animation, but then also set the renaming identifier on the entity to RadarAnimation so Core Data knows what it used to be called.
Incidentally attributes and relationships can also have renaming identifiers, so they can also be renamed this way.
Apple provides detailed documentation on what can be done with lightweight migration which will probably be useful as you go through this.

Execute a code after data model migration in Core Data (MagicalRecord)

I have to data models and a mapping model.
I would like to execute some code after the migration is done between my model v11 and model v12.
How can I do that? I'm using Magical Record.
I tried to do it in endEntityMapping from NSEntityMigrationPolicy, but it is an action that should be done after the entire migration, not in the middle.
Another idea I have is to save a flag in NSUserDefaults in any endEntityMapping and use it (and remove the flag) after my Core Data stack is setup by Magical Record, but this seems a bit workaround.

Resources