通过 CoreDataGenerateAccessors 添加对象后,多对多关系的 NSSet 为空
我有两个核心数据实体:Parent
和 Child
。父级与名为 children
的 Child
具有多对关系。逆关系来自 Child.parent
。因此父级有 CoreDataGenerateAccessors
: - (void)addChildrenObject:(Child *)value;
和 - (void)addChildren:(NSSet *)value;
>。
问题:在我使用这些访问器之一添加 Child 并保存 ManagedObjectContext parent.children
后为空。同时,每个添加的 Child
的 parent
属性都指向 Parent
的正确实例,并且 NSFetchedResultsController
获取此类子项(谓词是 parent = %@,
) 。
怎么会这样呢?只是不知道如何调试这种奇怪的 CoreData 行为。
I have two Core Data entities: Parent
and Child
. Parent has to-many relationship to a Child
called children
. Inverse relationship from is Child.parent
. So parent has CoreDataGeneratedAccessors
: - (void)addChildrenObject:(Child *)value;
and - (void)addChildren:(NSSet *)value;
.
Problem: after I add Child(s) by using one of those accessors and save managedObjectContext parent.children
is empty. At the same time parent
property of every added Child
point to proper instance of Parent
and NSFetchedResultsController
fetches such children (predicate is parent = %@, <instance of Parent>
) well.
How can it be so? Just don't have a clue how to debug such a strange CoreData behavior.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
解决了。不知何故,该集合的属性是由 .m 文件中的
@synthesize
而不是@dynamic
合成的。我知道这是一个非常愚蠢的拼写错误,但我想知道为什么 XCode 甚至没有生成有关此的警告!静态分析器也没有说什么!Solved. Somehow property of that set was sythesized by
@synthesize
not@dynamic
in .m file. I know this was a very stupid typo, but I wonder why XCode did not even generated a warning about that! Static analyzer said nothing about it too!当您定义这样的属性时,也会发生同样的情况:
正确的接口应该如下所示:
The same happens when you define properties like this:
Correct interface should look like this: