15
votes

I am learning Core Data so I was confused about the following.

I need to know how the relationships worked in the inherited scenario.

I have a data model with Entities Zoo, Animal , Dog, Lion.

Dog and Lion have Animal as the parent Entity.

Now Zoo has one to many relationship to animal.

I want to know if Zoo will need to have one to many relationship to animal or it need to have to one to many relationship to Dog and Lion. Will the inheritance work in this case or not. Like in future if I add Tiger, Do I need to add that in Zoo relationship or it will work just fine once I inherit Tiger from Animal.

1

1 Answers

19
votes

As long as your child entities inherit from the Animal entity, they will also inherit the relationship that the Animal parent entity has with Zoo. It's like class inheritance in OOP, where subclasses inherit variables, methods, etc from their superclasses automatically.

So all you need to do is declare that relationship between Zoo and Animal in your data model, and any new entities you add which extend Animal will have the same relationship with Zoo — there is no extra work to be done to ensure this.

See the Managed Object Models section of Apple's Core Data Programming Guide for details.