2013-02-07 59 views
0

我有一個用戶屬於限制基於組信息的CoreData模型數據結構是這樣的:CoreData NSPredicate有兩個一個一對多的關係

類別< - >>資訊< - >>組。

我有一個UserGroups對象的NSSet。我希望能夠根據Group對象的NSSet過濾類別,例如,如果一個類別不包含任何在我的NSSet中有任何組的信息,它們將不會被我的謂詞返回。

的信息,我可以做

[NSPredicate predicateWithFormat:@"ANY groups in (%@)",groups]; 

對於類別我試着只崩潰如下:

[NSPredicate predicateWithFormat:@"ANY information.groups in (%@)",groups]; 

但我需要寫在類別級別的謂詞。我在編程時假設我的數據集中的信息足夠大,以致於無法將它們全部取出並處理它們以查找我的類別。我想創建謂詞,它只會根據他/她的組獲取與用戶相關的類別。

感謝您的幫助!

回答

3

分類以下謂詞應該工作(假定information類的一對多關係信息):

[NSPredicate predicateWithFormat:@"SUBQUERY(information, $i, ANY $i.groups in %@)[email protected] > 0",groups]; 

或者,你可以使用反向關係:

// your set of Group objects: 
NSSet *groups = ...; 
// all Information objects that are related to any group object: 
NSSet *information = [groups valueForKey:@"information"] ; 
// all Category objects that are related to any information object: 
NSSet *categories = [information valueForKey:@"category"]; 

它可以結合到

NSSet *categories = [groups valueForKeyPath:@"information.category"]; 

這種替代解決方案的缺點可能是它也會在內存中創建組的中間組。

+0

頂部查詢很好用!萬分感謝! – tslater

相關問題