After adding objects via CoreDataGeneratedAccessor

2019-09-11 04:26发布

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.

2条回答
Emotional °昔
2楼-- · 2019-09-11 05:08

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;
查看更多
叼着烟拽天下
3楼-- · 2019-09-11 05:23

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!

查看更多
登录 后发表回答