On 5 Jan 2009, at 7:15 pm, Per Ohlson wrote:

I have a relationship similar to the following model in my program:

http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaFundamentals/Art/object_graph.gif

To access the objects corresponding to the collection of "employees" from the "department", I just iterate through the collection and fetch the object. But what is the best way to access the "department" from an "employee"?


There's no reason not to use a back-pointer. Just don't create a retain cycle. In other words, decide who owns what - in this case, 'department' would own 'employees' I guess - so the reference back to department would be weak, i.e. unretained.

I typically set back-pointers when objects are added to collections, so I'd have a department method:

- (void)        addEmployee:(Employee*) emp
{
        [mEmployeesArray addObject:emp];
        [emp setDepartment:self];
}


and an employee method:

- (void)        seDepartment:(Department*) dept
{
        mDeptRef = dept;
}

hth,

Graham


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to