After adding objects via CoreDataGeneratedAccessor

2019-09-11 05:03发布

问题:

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;