After adding objects via CoreDataGeneratedAccessors to-many relationships's NSSet is empty

冷暖自知 提交于 2020-01-06 18:36:40

问题


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.


回答1:


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!




回答2:


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;


来源:https://stackoverflow.com/questions/5432771/after-adding-objects-via-coredatageneratedaccessors-to-many-relationshipss-nsse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!