2013-11-27 54 views
2

我有我認爲是一個簡單的問題。我試圖過濾一些核心數據,其中有一個與子對象具有多對多關係的父對象,並且該子對象具有字符串ID。我想要獲取所有沒有子對象具有特定ID的父對象。NSPredicate篩選器與許多子對象屬性

我試圖!(ANY... LIKE)以及!(ANY..==)NONE與像和==和ALL children.id != otherid

我的查詢是這樣的:

NSFetchRequest* fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Parent"]; 

NSPredicate* predicate = [NSPredicate predicateWithFormat:@"NONE children.id LIKE %@",otherID]; 
[fetchRequest setPredicate: predicate]; 
NSError* error; 
NSArray* allParents = [[DataManager context] executeFetchRequest:fetchRequest error:&error]; 
//sanity check the predicate 
for (Parent* p in allParents) { 
    for (Child* c in p.children) { 
     if([c.id isEqualToString:otherID]){ 
      NSLog(@"PREDICATE FAIL!"); 
     } 
    } 
} 

我失去了與NSPredicate的東西嗎?這種類型的過濾是否允許CoreData?解決方案更好

+0

的可能重複[具有多對多關係的核心數據NSPredicate](http://stackoverflow.com/questions/15722930/core-data-nspredicate-with-to- many-relationship) - 摘要:這是一個核心數據錯誤,你可以使用SUBQUERY作爲解決方法。 –

回答

1

我發現了一個類似的問題,雖然不太明顯。事實證明,答案是棘手的SUBQUERY。這是什麼導致我就追:

NSPredicate Aggregate Operations with NONE

約SUBQUERY一個更開放的解釋一下:

http://funwithobjc.tumblr.com/post/2726166818/what-the-heck-is-subquery

產生的斷言是:

//in children get a $child and group all the $child objects together 
//where the ids match, if that groups count is 0 we know 
//the parent has no child with that id 
[NSPredicate predicateWithFormat: 
    @"SUBQUERY(children, $child, $child.id == %@)[email protected] == 0",objectId];