I want to build a category entity in core data. entityName=Category. It has 2 relationships called categoryParents and categoryChildren Those relationships are both configured as "to-many" and they are reverse of each other. They both destined to entity "Category" itself. I want to create a recursive category entity which child category may have more than 1 parent category.
I want to get root categories which do not have any parent categories.
But, I get 'to-many key not allowed here' error
Here is what I am trying
-(NSArray *) getCategoriesRoot
{
// Construct a fetch request
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Category" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
//NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(categoryActive == %@) AND (categoryParents == nil)", [NSNumber numberWithBool:YES]];
//NSPredicate *predicate = [NSPredicate predicateWithFormat:@"categoryParents.count = nil"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"categoryParents = nil"];
//NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(categoryActive == %@)", [NSNumber numberWithBool:YES]];
[fetchRequest setPredicate:predicate];
NSError *error = nil;
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
if ([fetchedObjects count]!=0)
{
return fetchedObjects;
}
else
{
return nil;
}
}
Here is what I get
2013-07-15 17:29:12.238 Basf Katalog[10206:c07] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'to-many key not allowed here' * First throw call stack: (0x1faa012 0x13e7e7e 0x10f2bd9 0x10f25f2 0x10f23c0 0x10f2195 0x10f1977 0x10f1464 0x10f05dd 0x10ee539 0x3999 0x37e3 0x22f4 0x19157 0x19747 0x1a94b 0x2bcb5 0x2cbeb 0x1e698 0x1f05df9 0x1f05ad0 0x1f1fbf5 0x1f1f962 0x1f50bb6 0x1f4ff44 0x1f4fe1b 0x1a17a 0x1bffc 0x207d 0x1fa5) libc++abi.dylib: terminate called throwing an exception
Here is the core-data
It is the first time i post to stack overflow so I can not upload screenshot of entity, yet! Basically I want an entity which has to relationships inverse of each other and pointing to entity itself. They are both to-many categoryParents <<------>> categoryChildren One category may have many parent categories One category may have many child categories
I am new to Core Data. Pls help!