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

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.

Related

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.

Core Data Relationships are not migrated (Custom Migration)

I really don't know where else to search for a solution to this problem. Basically, I've read twice (or more) all documentation and all pages I found on the web about Core Data Migration.
I had to change some names on the Entities (to readability) and also had to change the domain values used in my entities. AFAIK, in this case, I have to make a custom migration because I have to analyze the Input and generate a new Output:
I've created the new Model Version (v7)
I've updated the Model
Renamed existent Entities, Attributes and Relationships.
Added/Removed some Attributes
I've created the Mapping Model (v6 to v7)
I've configured the Mapping Model using Expressions
I've created two NSEntityMigrationPolicy (one for each entity)
The migration is going well for the entities and fields values, but none of the relationships are getting restored during the process.
During the process, the expression:
FUNCTION($manager, "destinationInstancesForEntityMappingNamed:sourceInstances:" , "RecentCallToRecentRecord", $source.recents)
is returning nothing.
I have debugged my custom NSEntityMigrationPolicy to check if the Source and Destination Entities are bound as expected and something very weird is happing. During the execution of createDestinationInstancesForSourceInstance:entityMapping:manager:error: everything is OK, after calling the superclass, I can navigate from Source to Destination (and the other way around). But during the execution of createRelationshipsForDestinationInstance:entityMapping:manager:error: this objects navigation does not work anymore.
Thanks in advance for any help.

Do i need a core data migration when i add a new model to my .xcdatamodeld file

i have a an .xcdatamodeld which already has 2 entities, i have added another entity into this file(am not sure whether this is the right way to add a new entity), anyways my question is, do i need to implement any kind of migrations to take care of users who already have the app installed on their devices.
Please note, i have not modified any columns or schema on already existing entities.
cheers.
You don't need to do migration if you just only adding new entity, assuming that it doesn't have relationship with the other. According to raywenderlich.com, you have to do migration for the following scenario:
Deleting entities, attributes or relationships.
Renaming entities, attributes or relationships using the renamingIdentifier.
Adding a new, optional attribute.
Adding a new, required attribute with a default value.
Changing an optional attribute to non-optional and specifying a default value.
Changing a non-optional attribute to optional.
Changing the entity hierarchy.
Adding a new parent entity and moving attributes up or down the hierarchy.
Changing a relationship from to-one to to-many.
Changing a relationship from non-ordered to-many to ordered to-many (and vice versa).

CoreData: Create a new entity to be parent of old entities

Recently, I am trying to refine my code. I found that all my entities has attributes named identifier and owner, so I want to create a entity to be their parent which contains identifier and owner.
Following is the result, all object inherit from a parent named SRModel, which has identifier and owner attributes.
However, after I delete all these redundant properties, the persistent store is not able to auto migration.
How can I solve the problem? Do I have to do migration by myself?
Are there any simple way to do so?
According to Apple's Core Data Model Versioning and Data Migration Programming Guide, you can't do that automatically.
You cannot, however, merge entity hierarchies; if two existing entities do not share a common parent in the source, they cannot share a common parent in the destination
Note Andy Riordan's point about inheritance. And don't just take his word for it; look at the generated .SQLite files yourself under the old and new models. Adding a parent entity with only two common attributes will just make your entities, and backing tables, larger, with no performance benefit. If you really want to note the two common elements, use a Protocol to call them out.

Core Data modeler not updating when entity names change

I am having an issue using Core Data modeler. I had an issue where I needed to change the names of entities in the modeler. When I did so, I deleted the associated managed object subclasses, did a clean, then went back the modeler, highlighted the entities with the new names, then generated subclasses using
Editor > Create NSManagedObject Subclass
When I did so, the newly generated subclasses still have the original names. Am I missing something?
In the model editor, you set the class name separately from the entity name. They don't have to be the same. If you only change the entity name name, what you're seeing is normal.
Select the ENTITY you want to change
Select Data Model from right hand top corner as shown below, and change as u want. Entity name and Class no need to be the same.
Finally Clean and Build the project.

Resources