I have simple tree structure in one CoreData entity called "Item".
Any item can be parent of another item and can have multiple children.
There's following relationships in the entity:
childItems: To-Many, Destination: Item, inverse: parentItem. optional parentItem: Destination: Item, inverse: childItems. optional
I am using Xcode-generated following Item.h :
@interface Item : NSManagedObject
{
}
@property (nonatomic, retain) NSString * title;
@property (nonatomic, retain) Item * parentItem;
@property (nonatomic, retain) NSSet* childItems;
@end
@interface Item (CoreDataGeneratedAccessors)
- (void)addChildItemsObject:(Item *)value;
- (void)removeChildItemsObject:(Item *)value;
- (void)addChildItems:(NSSet *)value;
- (void)removeChildItems:(NSSet *)value;
@end
The fetch request is very straightforward:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Item" inManagedObjectContext:[self managedObjectContext]];
[fetchRequest setEntity:entity];
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:NO];
[fetchRequest setSortDescriptors:[NSArray arrayWithObjects: sort, nil]];
[fetchRequest setFetchBatchSize:20];
NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:[self managedObjectContext] sectionNameKeyPath:nil
cacheName:@"AllItemsCache"];
When I try to set relationship it simply crashes. Why?
Item *item = [itemFetchedResultsController.fetchedObjects objectAtIndex:0];
[item addChildItemsObject:childItem];
debug output:
*** -[Item addChildItemsObject:]: unrecognized selector sent to instance 0x5494140
(gdb) po 0x5494140
<Item: 0x5494140> (entity: Item; id: 0x54921d0 <x-coredata://AB862620-04BE-4E42-84A6-8723455F5957/Item/p1> ; data: {
title = Test123;
})
I can set or get other properties just fine. but when I try to change relationships it crashes. What am I doing wrong?