我有一个与B有多对多关系的对象A.B有一个属性A,它与A的NSSet成反比关系。
伪代码:
class A{ NSSet *bConnections; } class B{ A *aConnection; }在模型中,我将关系设置为可选。 对于A,关系被定义为多个,B处的连接的倒数和删除规则级联。
B定义为bConnection的可选逆,删除规则无效。
当我创建对象,并使用[aInstance setValue:setOfBs forKey:@"bConnections"]将A的NSSet添加到A时,B中的反向关系不会自动设置。 我究竟做错了什么? 它不应该从Core Data自动更新吗?
这些对象包含: initWithEntity:entityDescription insertIntoManagedObjectContext:managedObjectContext
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
最满意答案
您需要使用mutableSetValueForKey:来获取关系的现有集合。 然后,您可以修改该集合,并自动设置正确的键值观察方法,以便维护对象图形的完整性。
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.
更多推荐
发布评论