views:

32

answers:

2

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?

+1  A: 

I would guess it is a naming collision caused by using a generic word like "Item" as a class name. Try refactoring the name to something more unique.

Alternatively, you might try verifying that the class does respond to method:

Item *item = [itemFetchedResultsController.fetchedObjects objectAtIndex:0];
NSLog(@"responds=%@",[item respondsToSelector:@selector(addChildItemsObject:)] ?@"YES":@"NO");
[item addChildItemsObject:childItem];

or investigate the class of the object returned directly:

NSLog(@"class=%@",[[itemFetchedResultsController.fetchedObjects objectAtIndex:0] class]);

It's possible that your actually returning a generic NSManagedObject that is being silently cast to its Item subclass. Unlikely, but you might as well check.

TechZen
A: 

Found the cause of the problem.

CoreData model and data classes only work if they are inside your project. IF you put them into dependent project it will have the strange errors like above.

My .xcdatamodel and entity classes was in other project and other project added to main project.

As soon as I moved everything into main project and removed dependency it works fine.

Rod