Core Data getting many data from relationship.

  • I think this is a very basic question and I'm just not finding the
    answer. I want to get the many data for a given relationship. Lets
    say I have a Department and I want to get all its Employees.
    Department has a relationship called employees. Is this what I do?

    id result = [aDepertment valueForKey:@"employees"];

    What do I get back? Is it an array, a set, or some other kind of
    container?

    Should I be using mutableArrayValueForKey from NSKeyValueCoding
    protocal?

    I have been reading the Core Data docs and all the examples show
    situations that return only one object.

    thanks for the help

    -dave
  • On Oct 6, 2006, at 1:28 PM, David Alter wrote:

    > I think this is a very basic question and I'm just not finding the
    > answer. I want to get the many data for a given relationship. Lets
    > say I have a Department and I want to get all its Employees.
    > Department has a relationship called employees. Is this what I do?
    >
    > id result = [aDepertment valueForKey:@"employees"];

    Yes, this should be spelled out in the Core Data documentation.  All
    basic property (attribute, to-one or to-many relationship, or fetched
    property) manipulation can be performed using key-value coding.

    > What do I get back? Is it an array, a set, or some other kind of
    > container?

    You get an NSSet back from that method.  Since it's an NSSet, you can
    only access the to-many relationship that way, not manipulate it.

    > Should I be using mutableArrayValueForKey from NSKeyValueCoding
    > protocal?

    Actually, to manipulate a relationship rather than just access it, you
    should use -mutableSetValueForKey:.  Core Data follows the relational
    model and thus relationships are unordered; order is imposed upon
    data, not inherent in it.

    -mutableSetValueForKey: will return an NSMutableSet that represents
    the relationship, and which you can manipulate in order to manipulate
    the relationship.  All necessary inverse relationships etc. will be
    maintained while you perform this manipulation, and any accessor/
    mutator methods defined for the relationship will also be invoked as
    appropriate.

    > I have been reading the Core Data docs and all the examples show
    > situations that return only one object.

    Please file a bug report or use the feedback form in the web site
    documentation to list the specific examples you're looking at, and
    that you would like to see examples of relationship manipulation in
    them as well.

      -- Chris
  • On Oct 6, 2006, at 1:28 PM, David Alter wrote:

    > I want to get the many data for a given relationship. Lets say I
    > have a Department and I want to get all its Employees. Department
    > has a relationship called employees. Is this what I do?
    > id result = [aDepertment valueForKey:@"employees"];
    > What do I get back? Is it an array, a set, or some other kind of
    > container?
    > Should I be using mutableArrayValueForKey from NSKeyValueCoding
    > protocal?
    > I have been reading the Core Data docs and all the examples show
    > situations that return only one object.
    >
    <http://developer.apple.com/documentation/Cocoa/Conceptual/CoreData/Articles
    /cdFAQ.html#//apple_ref/doc/uid/TP40001802-242985
    >
    <http://developer.apple.com/documentation/Cocoa/Conceptual/CoreData/Articles
    /cdRelationships.html#//apple_ref/doc/uid/TP40001857-SW3
    >

    mmalc
  • id result = [aDepertment valueForKey:@"employees"];
    NSLog(@"Result is %@", [result class]);

    You will probably get an NSSet back if it is a to-many relationship.
    The logging will confirm what you are getting back.

    Marcus S. Zarra
    Zarra Studios LLC
    Simply Elegant Software for OS X
    www.zarrastudios.com

    On Oct 6, 2006, at 2:28 PM, David Alter wrote:

    > I think this is a very basic question and I'm just not finding the
    > answer. I want to get the many data for a given relationship. Lets
    > say I have a Department and I want to get all its Employees.
    > Department has a relationship called employees. Is this what I do?
    >
    > id result = [aDepertment valueForKey:@"employees"];
    >
    > What do I get back? Is it an array, a set, or some other kind of
    > container?
    >
    > Should I be using mutableArrayValueForKey from NSKeyValueCoding
    > protocal?
    >
    > I have been reading the Core Data docs and all the examples show
    > situations that return only one object.
    >
    > thanks for the help
    >
    > -dave
    >
    >
    > _______________________________________________
    > Do not post admin requests to the list. They will be ignored.
    > Cocoa-dev mailing list      (<Cocoa-dev...>)
    > Help/Unsubscribe/Update your Subscription:
    > http://lists.apple.com/mailman/options/cocoa-dev/<mzarra...>
    >
    > This email sent to <mzarra...>
previous month october 2006 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