Reputation: 12003
I'm trying to figure out if the KVC mechanisms provide any sort of help when dealing with relationship properties which are inverse relationships of each other. I'll use the contrived and standard Department/Employee example.
@interface Department : NSObject {
NSMutableArray *employees;
}
@property (retain) NSMutableArray *employees;
// KVC methods for mutating the employees array
@end
@interface Employee : NSObject {
Department *department;
}
@property (retain) Department *department;
With just this (and the omitted KVC collection methods) what kind of help does KVC provide for managing the relationship in both directions? I'm only at the conceptual stage right now. I know with Core Data I can set explicit inverse relationships and thus when I use [myDepartment insertObject:newEmployee inEmployeesAtIndex:lastIndex];
, then newEmployee.department
is automatically set to myDepartment
, but can I achieve this with solely KVC and the runtime, or do I need Core Data?
Thanks in advance for helping.
Edit Somewhat unrelated but also important, in my code I put Employee's property to Department as being retain
but I'm wondering if this will cause a retain cycle?
Upvotes: 1
Views: 222
Reputation: 55116
There's nothing directly in Key-Value Coding that will help you maintain inverse relationships. You could leverage Key-Value Observing to do so however.
The main things you'll want to do are:
Given all of that, you'll probably just want to use Core Data, because it already does all of that and has been extensively tuned over the years to do it quite well.
Upvotes: 1