开发者

Core Data inverse relation does not get set

开发者 https://www.devze.com 2023-01-14 19:33 出处:网络
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.

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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消