views:

46

answers:

2

I have an object A with a to-many relation to B. B has a property A that is the inverse relation to A's NSSet.

Pseudo code:

class A{
    NSSet *bConnections;
}

class B{
    A *aConnection;
}

In the model I have set up the relationship as optional. For A, the relationship is defines as to-many, inverse of aConnection at B and delete rule cascade.

B is defined with optional, inverse of bConnection and delete rule nullify.

When I create the objects, and add a NSSet of Bs to A with [aInstance setValue:setOfBs forKey:@"bConnections"] the inverse relation in B is not set automatically. What am I doing wrong? Should not it be updated automatically from Core Data?

The objects are inited with: initWithEntity:entityDescription insertIntoManagedObjectContext:managedObjectContext

+1  A: 

I may have found the solution; I did update the existing NSSet returned from A to add new Bs. When I alloc a new NSSet and adds objects, the references seems to be ok. The objects are created from an external JSON service, and I thought I could just update the existing set of Bs in A, but then the inverse relations are not set correctly.

Andi
+1  A: 

You need to use mutableSetValueForKey: to get the existing set for the relationship. You then modify that that set and the proper key-value observing methods are automatically set so that the object graph integrity is maintained.

TechZen