簡體   English   中英

我想要一個核心數據實體,該實體可以通過多對多關系指向自己。 類別實體

[英]I want a core data entity which could relationally point to itself with many-to-many relationships. Category entity

我想在核心數據中建立一個類別實體。 entityName =類別。 它具有2個稱為categoryParents和categoryChildren的關系。這些關系都配置為“一對多”,並且彼此相反。 他們倆都注定要成為實體“類別”本身。 我想創建一個遞歸類別實體,其子類別可能具有多個父類別。

我想獲取沒有任何父類別的根類別。

但是,我收到“此處不允許多對鍵”錯誤

這是我正在嘗試的

-(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;
    }
}

這就是我得到的

2013-07-15 17:29:12.238 Basf Katalog [10206:c07] *由於未捕獲的異常'NSInvalidArgumentException'而終止應用程序,原因:'此處不允許使用to-many鍵'*第一個拋出調用堆棧:(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:終止稱為拋出異常

這是核心數據

這是我第一次發布堆棧溢出消息,因此我還不能上傳實體的屏幕截圖! 基本上,我想要一個實體,該實體必須具有彼此相反的關系並指向實體本身。 它們都是一對多類別父<< ------ >>類別孩子一個類別可能有很多父類別一個類別可能有很多子類別


我是Core Data的新手。 請幫助!

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

應該管用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM