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

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).

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 with unique constraints and relationships-IOS

I have a core data design with multiple tables using relationships. My database is SQLite. For updates I import data from JSON and use this method:
[NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:context].
I have added unique constraints in core data.
If I update an entity that is a relationship of another object it loses the connection.
Ex: Entity "person" that contains the one to one relationship to "pet_id". If I update "pet" it changes his id and "person" still points to the old id, so they are not related any more.
Is there a way to avoid this problem?
I don't think this is documented anywhere yet. Here's what it sounds like is happening:
You create a new instance. Your constraints mean that this instance matches an existing instance. But...
Your new instance has a nil value for this relationship. So...
The existing instance's value for the relationship is replaced by this new nil value.
To maintain the relationship, your new instance needs to already have the correct value for that relationship. You're essentially asking that the constraint matching system ignore the fact that the relationship value is different in your new instance, but to accept new values for other attributes.
I think what you're expecting is completely reasonable but I'm also not surprised that the current implementation doesn't support it. I recommend filing a bug with Apple about this, and investigating non-constraint based approaches to keeping your data unique.

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.

Adding a record into an association table using Breezejs

Background Info
So I am using Breezejs and Knockout with EF5 and the Breeze MVC api controller on the backend. One of my tables in my data base is an association table, 3 columns - an id and two foreign keys(we'll call them fkey1 and fkey2 with table1 and table2). In my application, I need to add a record to this association table. Breeze knows about the relationships that this table has.
Situation
Breeze js makes a new record for me, then I find out the records I need to associate with it. Do I need to add in the id of the table1 entity into the fkey1 observable and the table2 entity id into the fkey2 observable AND add the whole entities into their respective relationship properties AND add push this new entity object into the table1 and table2 entities association property? Or does adding the id's into the new object automatically add those objects into the relationship properties (maybe those objects are subscribed to the fkey1 and fkey2 properties? - this is what I'm guessing happens in the background of breeze, a shot in the dark though I have no idea).
Creating new entities with Breeze is super easy and I love it, but I'm a little confused when it comes to creating new entities that have a lot of relationships.
Let me if you need a better description of my situation, it's kind of a tough thing to explain. Thanks!
Providing that you set the "foreign key" properties appropriately when creating and attaching entities, Breeze will automatically update all of the associated relationships, i.e. navigation properties on this and any related entities. You should never need to manually perform any fixup.
Similarly, if you do the reverse and assign an entity to a scalar navigation property then Breeze will automatically update the foreign key(s). For a collection navigation property, if you push a value into the collection then Breeze will automatically update the foreign key of the entity being pushed.
Hope this helps, but maybe I'm missing the question...

Replace entities in to-many relationship in Core Data

When I retrieve an entity from the one side of a one-to many relationship, I create a mutable array from the set that is the collection of entities from the relationship. I manipulate, edit or otherwise change those entities, possibly delete existing or add new.
When through with the changes I simply use the array to create a new set then replace the original set with that which I created like so:
self.myOneSideEntity.theManySideEntitiesRelationship = [NSSet setWithArray:myNewArrayOfEntities];
It occurred to me that replacing the set may not be deleting the old members. What happened to them? Is this the proper way to edit the collection of related objects? Am I leaving any kind of orphans or going against best practices with this technique?
My relationship is set up with an inverse, cascade delete on the one side, nullify on the many side and the inverse relationship is not optional.
I've spent some days to understand similar behavior in my application.
Relation's "Delete Rule" works only when the object that contains relation is deleted itself. If you simply replace one set of objects with another (as you do) - nothing happens. Child objects that were in old set will simply have inverse relations set to nil. So if that relation (from child side) is not optional, you will get CoreData error when saving context.
For now I didn't find any way to manage this, except manual deletion of old objects.
For me the issue was with getting objects which were wired with current object. (groupObject.docs)
It was solved when I add context by which I get this data.
I'm using MagicalRecord:
[GroupObject MR_findAllInContext:[NSManagedObjectContext MR_defaultContext]]
instead of
[GroupObject MR_findAll]

Resources