Core Data: Cross-model relationships

  • I have some entities in my application that are global (like
    preferences), and remain the same across application runs. These can
    be referred to by entities that are specific to each stored document.
    Thus I have a model which has a list of states that entities in each
    separate document can be in. There is also a document model, which
    describes the specific entities stored in each document.

    Thus the entities in the document model refer to the state entities
    in the preferences model. Now I can't use a relationship, because
    Core Data does not support cross-store relationships. So I'm trying
    to use a Fetched Property with a Fetch Request to define a weak cross-
    store (and therefore cross-model) relationship.

    Is this theoretically possible?

    When I setup a fetched property for state in my document entity (in
    Xcode modeller) it needs the destination set – however, the
    destination popup only shows entities in the current model. What I
    want (I think) is a predicate something like:

    State.id == $FETCH_SOURCE.state_id

    in order to relate the document entity to the globally defined state
    entity in the other model.

    If this isn't possible to setup in Xcode, can I do it programmatically?

    Thanks

    Ian
  • Since no one has had a go at this one, I'll assume that the answer is
    no, you cannot set up cross-model relationships (or fetched
    properties) in Xcode modeller.

    I think the way to do it is in code setting up a new
    NSFetchedPropertyDescription with an appropriate NSFetchRequest.
    Should know more tomorrow about how this works.

    Thanks
    Ian

    On 12/10/2007, at 4:40 PM, Ian Joyner wrote:

    > I have some entities in my application that are global (like
    > preferences), and remain the same across application runs. These
    > can be referred to by entities that are specific to each stored
    > document. Thus I have a model which has a list of states that
    > entities in each separate document can be in. There is also a
    > document model, which describes the specific entities stored in
    > each document.
    >
    > Thus the entities in the document model refer to the state entities
    > in the preferences model. Now I can't use a relationship, because
    > Core Data does not support cross-store relationships. So I'm trying
    > to use a Fetched Property with a Fetch Request to define a weak
    > cross-store (and therefore cross-model) relationship.
    >
    > Is this theoretically possible?
    >
    > When I setup a fetched property for state in my document entity (in
    > Xcode modeller) it needs the destination set – however, the
    > destination popup only shows entities in the current model. What I
    > want (I think) is a predicate something like:
    >
    > State.id == $FETCH_SOURCE.state_id
    >
    > in order to relate the document entity to the globally defined
    > state entity in the other model.
    >
    > If this isn't possible to setup in Xcode, can I do it
    > programmatically?
    >
    > Thanks
    >
    > Ian
    >
  • On Oct 11, 2007, at 11:40 PM, Ian Joyner wrote:

    > When I setup a fetched property for state in my document entity (in
    > Xcode modeller) it needs the destination set – however, the
    > destination popup only shows entities in the current model. What I
    > want (I think) is a predicate something like:
    >
    > State.id == $FETCH_SOURCE.state_id

    I think you're far better off simulating this in code rather than
    trying to represent it in the model in any form. In other words,
    focus on getting the *values* transferred between stores rather than
    actual managed objects.

    I'm guessing you have something long the lines of key/value pairs for
    a dropdown in the UI -- with the options being defined in the prefs
    but selectable in each document?

    Create a method that fetches the managed objects from the prefs store
    and converts them into generic NSDictionary objects which can be used
    to populate the UI in the document. If the prefs change, post a
    notification so that all open documents can sort it out -- and do the
    same when you open a document.

    You can generate UUIDs if you want to assign unique ids to items you
    create in prefs. But keep in mind the basic issue with all of this is
    that the document themselves are not portable. That is, you can't
    take them to another machine since the prefs will be missing.

    I know this is all somewhat vague, but I have no idea what the code
    looks like.  :)

          - Scott
previous month october 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 31        
Go to today