Backward Relationship Management without CoreData

This is an Objective-J / Cappuccino question, but I added the cocoa tag since the structures are so similar.

One of the drawbacks of Cappuccino is that CoreData hasn't been migrated yet, so you have to make all your model objects manually.

In CoreData, your backward relationships will take over automatically for you ... if you add an object to many relationships in another object, you can traverse the graph in both directions.

Without CoreData, is there any clean way to automatically establish these backward relationships?

For a more specific example, let's look at a typical example of a department and employees. To use rails terminology, there are many employees in the Department object and the employee belongs to a department.

So our model for our department has an NSMutableSet (or CPMutableSet) "employees" that contains a set of employees, and our Employee model has a variable "department" that points to the Department model that owns it.

Is there a simple way to make it so that when a new Employee model is added to the set, the feedback (employee.department) is automatically set? Or vice versa: if I set the employee's department model, is it automatically added to this department employee?

I know correctly that I am making a "ValidatedModel" object that all of my subclasses of models that add multiple methods that establish a reverse relationship using KVO. But I am afraid that I am doing a lot of pointless work and that there is already an easier way to do it.

Can anyone put my troubles to rest?

+2


source to share


4 answers


I can't speak specifically with Objective-J, but the usual way to do it without Core Data is to set the feedback in the setter. So, using the employee / department example, you would do something like this:

- (void)setDepartment:(Department *)aDepartment {
    if (department == aDepartment)
        return;

    [department release];
    department = [aDepartment retain];

    [department addEmployee:self];
}

      



You need to make sure that you don't update your instance variable if the new value already matches the existing value. If you haven't done so, it setDepartment:

will call it addEmployee:

, but it addEmployee:

will call it setDepartment:

in an infinite loop.

Also note that this is an open prompt for save loops. It depends on how your model is structured, but the object that "owns" the other is the one that owes retain

it. So my example may not be the best, because it's probably more accurate to say that a department "owns" an employee.

+2


source


You probably want to establish a relationship in your setter. Using your example, the Objective-J code would be similar to this.

- (void)setDepartment:(Department)aDepartment {
    if (department === aDepartment)
        return;

    [department addEmployee:self];
}

      



As you can see, there is no need to save / release. Objective-J is built in javascript that collects garbage. All memory management methods are implemented, but they do nothing (except cluttering the code)

Also, since this is javascript, it is generally a good idea to check for equality like (===). For more information on type equality see http://www.webreference.com/js/column26/stricteq.html

+2


source


check out Cappuccino extensions from this 280 North employee: http://github.com/nciagra/Cappuccino-Extensions

It includes an ActiveRecord port. I haven't looked at this yet, but it might help you.

  • Johannes
+2


source


You can also check out this CoreData implementation at rbartolome. I only looked at it for a bit, but it looks like a start.

http://github.com/rbartolome/CoreData-Cappuccino

+1


source







All Articles