2014-06-29 56 views
0

我現在有以下核心數據模型:在覈心數據中,如何獲取NSSet的所有對象?

類別

@interface Category : NSManagedObject 
@property (nonatomic, retain) NSSet *subcategory; 

子目錄

@interface SubCategory : NSManagedObject 
@property (nonatomic, retain) Category *category; 
@property (nonatomic, retain) NSSet *items; 

項目

@interface Item : NSManagedObject 
@property (nonatomic, retain) SubCategory *subCategory; 

我想在一個類別中的所有項目,但當我寫:

NSLog(@"Items in Category:%@", self.category.subcategory.items) 

我什麼也沒得到。

如何獲取信息?因爲我有一個屬性接受類我在ViewController中添加:

@property (strong, nonatomic) Category *category; 

回答

0

一個可行的辦法是使用鍵 - 值編碼 Collection Operator @distinctUnionOfSets

NSSet *allItems = [self.category valueForKeyPath:@"[email protected]"]; 

(需要注意的是更好的名稱爲「子類別」的子對象將爲 「子類別」。)

或者,您可以對「Item」實體執行獲取請求wi使用逆關係TH謂詞 :

[NSPredicate predicateWithFormat:@"subCategory.category == %@", self.category]; 

這種方法的優點是,中間「子類別」對象 沒有加載到存儲器中。

+0

工程就像一個魅力!謝謝! – naSh

相關問題