Core Data entity inheritance --> limitations?

前端 未结 3 1490
一向
一向 2021-01-05 10:30

I thought I\'ll post this to the community. I am using coredata, and have two entities. Both entities have a hierarchical relationship. I am noticing quite a lot of duplic

相关标签:
3条回答
  • 2021-01-05 10:50

    I thought it would be useful to mention that the Notes app on iOS 10 uses inheritance in its Core Data model. They use a base entity SyncingObject, that has 7 sub-entities including Note and Folder. And as you mentioned all of these are stored in the same SQLite table which has a whopping 106 columns, and since are shared among all entities most are NULL. They also implemented the folder-notes one-to-many relation as a many-to-many which creates a pivot table, which might be a work-around for an inheritance problem.

    There are a couple of advantages to using entity inheritance that likely outweigh these storage limitations. For example, a unique constraint can be unique across entities. And a fetch request can return different entities making UI that uses fetched results controller simpler, e.g. grouping by accounts or folders in a sidebar.

    0 讨论(0)
  • 2021-01-05 10:56

    I think it's a mistake to draw to close a parallel between entities and classes. While very similar they do have some important differences.

    The most important difference is that entities don't have code like a class would so when you have entities with duplicate attributes, your not adding a lot of extra coding and potential for introducing bugs.

    A lot of people believe that class inheritance must parallel entity inheritance. It does not. As a long as a class descends from NSManagedObject and responds to the right key-value messages for the entity it represents, the class can have many merry adventures in it's inheritance that are not reflected in the entities inheritance. E.g. It's fairly common to create a custom base class right below NSManagedObject and the have all the subsequent managed object subclasses inherit from that regardless of their entities.

    I think the only time that entity inheritance is absolutely required is when you need different entities to show up in the same relationship. E.g:

    Owner{
      vehical<-->Vehical.owner
    }
    
    Vehical(abstract){
      owner<-->Owner.vehical
    }
    
    Motocycle:Vehical{ 
    
    }
    
    Car:Vehical{
    
    }
    

    Now the Owner.vehical can hold either a Motocycle object or a Car object. Note that the managed object class inheritance for Motocycle and Car don't have to be same. You could have something like Motocycle:TwoWheeled:NSManagedObject and Car:FourWheeled:NSManagedObject and everything would work fine.

    In the end, entities are just instructions to context to tell it how the object graph fits together. As long as your entity arrangement makes that happen, you have a lot flexibility in the design details, quite a bit more than you would have in an analogous situation with classes.

    0 讨论(0)
  • 2021-01-05 10:59

    I have had issues in the past with data migration of models that had inheritance - you may want to experiment with that and see if you can get it to work.

    As you noted also, all objects go in one table.

    However, as Core Data is managing an object graph, it is really nice to keep the structure the way you would naturally have it just modeling objects - which includes inheritance. There's a lot to be said for keeping the model sane so that you have to do less work in maintaining code.

    I have personally used a fairly complex CD model with inheritance in one of my own apps, and it has worked out OK (apart from as I said having issues with data migration, but that has been so flakey for me in general I do not rely on that working any longer).

    0 讨论(0)
提交回复
热议问题