2012-10-31 157 views
0

我有一個核心數據模型,有一對多的關係,有一個類別,它可以包含許多子類別。核心數據謂詞對許多

類別< ---- >>子目錄

我試圖執行一個取來檢查,如果一個特定的類別中包含有特定名稱的子目錄。假設我有以下兩個類別,我想要查看名爲「Fruits」的類別中是否有任何子類別名稱爲「Apple」。

Vetegables 
    - Carrot 
    - Lettuce 

Fruits 
    - Apple 
    - Orange 
    - Pear 

代碼:

- (SubCategory *)searchForSubCategoryWithName:(NSString *)subCategory 
            inCategory:(Category *)category 
{ 
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"SubCategory" inManagedObjectContext:self.beer.managedObjectContext]; 
    [fetchRequest setEntity:entity]; 

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == [c] %@", subCategory]; 
    [fetchRequest setPredicate:predicate]; 

    NSError *error; 
    NSArray *fetchedObjects = [self.beer.managedObjectContext executeFetchRequest:fetchRequest error:&error]; 

    if (fetchedObjects != nil && fetchedObjects.count > 0) { 
     return [fetchedObjects objectAtIndex:0]; 
    } 
    else { 
     return nil; 
    } 
} 
+0

:)對我來說看起來不錯,那麼你的問題是什麼? – Tobi

+0

NSPredicate * predicate = [NSPredicate predicateWithFormat:@「name == [c]%@ and category ==%@」,subCategory,category]; – NeverBe

回答

2

儘量尋找其中有你喜歡的不是其他方式,然後從返回結果category.subcategory子類別類別的實體。我不知道你的NSManagedObjects看怎麼樣,但試試這個:

- (SubCategory *)searchForSubCategoryWithName:(NSString *)subCategory 
            inCategory:(Category *)category 
{ 
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Category" inManagedObjectContext:self.beer.managedObjectContext]; 
    [fetchRequest setEntity:entity]; 

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"subcategory.name == [c] %@ AND name == %@", subCategory, category.name]; 
    [fetchRequest setPredicate:predicate]; 

    NSError *error; 
    NSArray *fetchedObjects = [self.beer.managedObjectContext executeFetchRequest:fetchRequest error:&error]; 

    if (fetchedObjects != nil && fetchedObjects.count > 0) { 
     Category *categoryManagedObject = [fetchedObjects objectAtIndex:0]; 
     return categoryManagedObject.subcategory; 
    } 
    else { 
     return nil; 
    } 
} 

...或者只是添加類別到您的謂詞,你應該在你的子類別管理對象定義:

- (SubCategory *)searchForSubCategoryWithName:(NSString *)subCategory 
             inCategory:(Category *)category 
    { 
     NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
     NSEntityDescription *entity = [NSEntityDescription entityForName:@"Category" inManagedObjectContext:self.beer.managedObjectContext]; 
     [fetchRequest setEntity:entity]; 

     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == [c] %@ AND category == %@", subCategory, category]; 
     [fetchRequest setPredicate:predicate]; 

     NSError *error; 
     NSArray *fetchedObjects = [self.beer.managedObjectContext executeFetchRequest:fetchRequest error:&error]; 

     if (fetchedObjects != nil && fetchedObjects.count > 0) { 
      return [fetchedObjects objectAtIndex:0]; 
     } 
     else { 
      return nil; 
     } 
    } 
+0

非常感謝,完美的工作 – Vikings