2013-03-07 119 views
0

假設兩個實體:EntityA和EntityB,通過多對多關係連接。假設關係屬性分別與相關實體B和相關實體A相關。我有一個NSArray的EntityB實例。我想查找所有與nsarray中的所有實例有關的EntityA實例。那就是如果數組是[entityB1,.....,entityBn]。找到與entityB1,wntityB2直到entityBn相關的所有EntityA實例。我怎麼寫這種謂詞?如果relatedEntityB包含陣列的單個對象,最後daddyPredicate組合它們都與OR核心數據多對多關係查詢

NSMutablArray *predicateArray = [NSMutableArray array]; 
for (int i=0;i<entityBArray.count;i++){ 
    NSPredicate *subPredicate = [NSPredicate predicateWithFormat:@"ANY relatedEntityBs contains %@",[entityBArray objectAtIndex:i]; 
    [predicateArray addObject:subPredicate] ; 
} 
NSCompoundPredicate *daddyPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:predicateArray]; 

基本上每個子謂詞檢查:

+0

是否要檢查是否相等,即查找與entityB1,...,entitiyBn相關的所有對象,而不是其他人?那麼這可能是有用的:http://stackoverflow.com/questions/13084930/how-to-create-a-core-data-predicate-to-test-that-a-relation-contains-all-given-o/ 13086353#13086353。 – 2013-03-07 12:14:24

回答

2

該斷言應該這樣做。

+0

謝謝,這有幫助。 – kyurkchyan 2013-03-08 11:31:12