通过 CoreDataGenerateAccessors 添加对象后,多对多关系的 NSSet 为空

发布于 2024-10-27 11:42:03 字数 626 浏览 3 评论 0原文

我有两个核心数据实体:ParentChild。父级与名为 childrenChild 具有多对关系。逆关系来自 Child.parent。因此父级有 CoreDataGenerateAccessors- (void)addChildrenObject:(Child *)value;- (void)addChildren:(NSSet *)value; >。

问题:在我使用这些访问器之一添加 Child 并保存 ManagedObjectContext parent.children 后为空。同时,每个添加的 Childparent 属性都指向 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陈甜 2024-11-03 11:42:03

解决了。不知何故,该集合的属性是由 .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!

九厘米的零° 2024-11-03 11:42:03

当您定义这样的属性时,也会发生同样的情况:

@interface Project : BaseModel {
  Workspace *workspace;
  NSString *name;
}

@property (nonatomic, retain) Workspace *workspace;
@property (nonatomic, retain) NSString *name;

正确的接口应该如下所示:

@interface Project : BaseModel {}

@property (nonatomic, retain) Workspace *workspace;
@property (nonatomic, retain) NSString *name;

The same happens when you define properties like this:

@interface Project : BaseModel {
  Workspace *workspace;
  NSString *name;
}

@property (nonatomic, retain) Workspace *workspace;
@property (nonatomic, retain) NSString *name;

Correct interface should look like this:

@interface Project : BaseModel {}

@property (nonatomic, retain) Workspace *workspace;
@property (nonatomic, retain) NSString *name;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文