核心数据反向关系未设置(Core Data inverse relation does not get set)

编程入门 行业动态 更新时间:2024-10-27 06:25:06
核心数据反向关系未设置(Core Data inverse relation does not get set)

我有一个与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.

更多推荐

本文发布于:2023-08-05 14:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1434737.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:核心   关系   数据   Core   set

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!