「アイテム」と呼ばれる 1 つの CoreData エンティティに単純なツリー構造があります。
どのアイテムも別のアイテムの親になることができ、複数の子を持つことができます。
エンティティには次の関係があります。
childItems: To-Many、Destination: Item、inverse: parentItem。オプションのparentItem:宛先:Item、逆:childItems。オプション
Xcode で生成された 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
フェッチ リクエストは非常に簡単です。
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"];
関係を設定しようとすると、単にクラッシュします。なんで?
Item *item = [itemFetchedResultsController.fetchedObjects objectAtIndex:0];
[item addChildItemsObject:childItem];
デバッグ出力:
*** -[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;
})
他のプロパティを問題なく設定または取得できます。しかし、関係を変更しようとするとクラッシュします。私は何を間違っていますか?