Core Data: Cross-Model Relationships

  • Hi,

    I am having a rather complex Core Data Model (27 entities so far and
    counting). While this is obviously no big deal for the engine at all,
    I a having reasons to split this into multiple models:

    - While the Xcode data model editor is nice to work with, it has (as
    typical for graphical editors) its issues concerning change control.
    The editor makes it very easy to change things unintentional, and the
    storage format makes it hard to compare two versions to check what
    changed.
    Separation into sub-models would keep changes more localized.

    - I need to make parts of the whole store exportable into separate
    files for exchange between users. My idea is to use small Core Data
    stores as exchange format. The models for these files are sub-models
    of the whole. Using the same model file for both the exchange store
    and as part of the whole model has the obvious advantage of less
    maintenance work and guaranteeing them being in sync.

    So far, so easy: +[NSManagedObjectModel modelByMergingModels:] will
    merge my sub-models nicely. But I need some cross-sub-model
    relationships. My idea is to create these relationships
    programmatically after the sub-models have been loaded and merged
    into one model.

    Questions:

    - Is this feasible and a good idea?
    - Does my whole consideration make sense?
    - Is there a better/easier way to achieve what I want?

    Thanks a lot for any thoughts

    Kai
  • Kai,

    > So far, so easy: +[NSManagedObjectModel modelByMergingModels:] will
    > merge my sub-models nicely. But I need some cross-sub-model
    > relationships. My idea is to create these relationships
    > programmatically after the sub-models have been loaded and merged
    > into one model.
    >
    > Questions:
    >
    > - Is this feasible and a good idea?
    > - Does my whole consideration make sense?
    > - Is there a better/easier way to achieve what I want?

    On the whole what you propose makes sense.  There is one delicate
    consideration.  When you open a store file, the model you use must
    match exactly the model used to create the store.  No more, no less.

    So two points come up.  Any programmatic alterations to a model must
    be maintained.  If you add some code, and then save some files, you
    can't change your mind later about what programmatic alterations to
    make without performing a schema migration.

    Secondly, the sub-stores and sub-models cannot at runtime actively
    use a subset of the primary model.  They either need to exactly use
    the primary model, or have their own NSPersistentStoreCoordinator
    with their own precise model.

    Or put another way, Core Data doesn't support dynamic plugins to the
    model without either a different coordinator, a schema migration
    step, or both.
    --

    -Ben
  • Ben,

    thanks for your answer.

    > Kai,
    >
    >> So far, so easy: +[NSManagedObjectModel modelByMergingModels:] will
    >> merge my sub-models nicely. But I need some cross-sub-model
    >> relationships. My idea is to create these relationships
    >> programmatically after the sub-models have been loaded and merged
    >> into one model.
    >>
    >> Questions:
    >>
    >> - Is this feasible and a good idea?
    >> - Does my whole consideration make sense?
    >> - Is there a better/easier way to achieve what I want?
    >
    > On the whole what you propose makes sense.  There is one delicate consideration.  When you open a store file, the model you use must match exactly the model used to create the store.  No more, no less.
    >
    > So two points come up.  Any programmatic alterations to a model must be maintained.  If you add some code, and then save some files, you can't change your mind later about what programmatic alterations to make without performing a schema migration.

    Yes, this was my understanding.

    >
    > Secondly, the sub-stores and sub-models cannot at runtime actively use a subset of the primary model.  They either need to exactly use the primary model, or have their own NSPersistentStoreCoordinator with their own precise model.

    Exactly. My idea is to load the sub-model(s) separately and create a whole separate Core Data stack to access export files.

    >
    > Or put another way, Core Data doesn't support dynamic plugins to the model without either a different coordinator, a schema migration step, or both.

    That is an interesting and important way to phrase it. While technically more than understandable, this is a very unfortunate restriction of Core Data for modern application development.

    Kai
previous month november 2007 next month
MTWTFSS
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    
Go to today