0

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!

Add080bbA
  • 1,818
  • 1
  • 18
  • 25

1 Answers1

0
[NSPredicate predicateWithFormat:@"categoryParents.@count = 0"];

should work.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • is the structure correct? May entity "Category" have 2 seperate releationship connections reverse of each other as many-to-many ? – Add080bbA Jul 16 '13 at 08:13
  • @user2583906: Yes, and it is documented here: ["Many-to-Many Relationships"](http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/CoreData/Articles/cdRelationships.html#//apple_ref/doc/uid/TP40001857-SW10). – Martin R Jul 16 '13 at 08:16
  • i am trying to vote you up but not enough reputation. Sorry ! :) – Add080bbA Jul 16 '13 at 17:08
  • sorry. as i said > new to the ways of stackoverflow – Add080bbA Jul 19 '13 at 16:00
  • i am trying to find child categories of a category – Add080bbA Jul 22 '13 at 18:39
  • -(NSArray *) getchildCategoriesOfaCategory:(Category *)aCategory { NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"Category" inManagedObjectContext:context]; [fetchRequest setEntity:entity]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(categoryParent = %@)" , aCategory]; [fetchRequest setPredicate:predicate]; – Add080bbA Jul 22 '13 at 18:43
  • i get exception 'NSInvalidArgumentException', reason: 'to-many key not allowed here' again – Add080bbA Jul 22 '13 at 18:43
  • sorry . forget about it . silly question. aCategory.categoryChildren gets it already. – Add080bbA Jul 22 '13 at 19:06