Core data versioning/migration after deleting entities - ios

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.

Related

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.

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.

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

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.

Create Core Data entities dynamically during runtime

I need to be able to create new core data entities during runtime. I've written the code to create the objects programmatically, however, I can't add the entities during runtime as the model is immutable.
My problem is similar to this post, however there is no satisfactory answer: How to dyanmic create a new entity (table) via CoreData model?
The documentation regarding changing the core data model explains:
Managed object models are editable until they are used by an object
graph manager (a managed object context or a persistent store
coordinator). This allows you to create or modify them dynamically.
However, once a model is being used, it must not be changed. This is
enforced at runtime—when the object manager first fetches data using a
model, the whole of that model becomes uneditable. Any attempt to
mutate a model or any of its sub-objects after that point causes an
exception to be thrown. If you need to modify a model that is in use,
create a copy, modify the copy, and then discard the objects with the
old model.
However, I'm unclear on what exactly this is saying--that the whole core data model can't be changed once the persistent store coordinator has been used or the attributes/etc of the individual entities can't be changed.
To be clear, I do not want to change the attributes of my current entities, I simply want to add new entities. It just seems weird to me to have to use migration to add new entities.
Any thoughts?
Thanks!
The documentation is pretty clear.
Copy the model.
Apply your changes to the new copy.
Destroy your old MOC, Persistent Store Coordinator, and all objects created from those.
Apply a migration, if necessary.
Create a new Core Data Stack (MOC, PSC, etc) using your updated model.
The migration could be a sticking point, but it should be do-able.

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.

Resources