2013-03-07 51 views
1

兩個模型類NSPredicate比較兩個集和組和用戶共享一個多到多關係多到許多關係

AUser 
- name 

AGroup 
- users 

我希望檢查兩組是否有着完全相同的用戶(沒有更多的用戶或用戶更少)

但不幸的是我收到以下錯誤:一對多這裏不允許

關鍵請注意,我查看了其他SO問題,但沒有一個似乎適合,因爲我試圖在過去2個小時內使用它們的方法,但是我無法使其工作。或者至少我沒有很好地理解他們。

也是我的謂詞是否可以比較兩個可變集?

- (BOOL)conversationExists:(NSMutableSet *)members { 
    NSFetchRequest *request= [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"AGroup" inManagedObjectContext:_managedObjectContext]; 
    NSPredicate *predicate =[NSPredicate predicateWithFormat:@"users==%@",members]; 
    //There is more code that I have not shown here as it is irrelevant to the question 
    //Mainly The NSPredicate line is the problem 
} 

非常感謝你的時間。對此,我真的非常感激。

從, 新的iOS程序員

回答

2

爲什麼它需要一個謂語?

[[groups valueForKey:@"users"] isEqualToSet:members]怎麼了?

+0

我只是覺得你需要將謂詞用於提取請求使用核心數據(或者至少我所看到的所有例子只使用了謂詞)。謝謝,順便說一下! – GangstaGraham 2013-03-07 21:03:58

+0

如果你使用'NSFetchRequest',你可以做,但你不需要使用'NSFetchRequest'。 – 2013-03-08 02:29:16

+0

這可能聽起來像一個愚蠢的問題。我只是好奇,在選中這個問題之前,是否檢查所有組? – GangstaGraham 2013-03-08 03:40:15

2

這篇文章有一個答案:How to use the "ALL" aggregate operation in a NSPredicate to filter a CoreData-based collection

本質上是這樣的:

[NSPredicate predicateWithFormat:@"SUBQUERY(members, $member, $member IN %@)[email protected] = %d", members, [members count]]; 
+0

請原諒我缺乏SQL知識,但'$ member'和'$ member IN'指的是什麼。另外,我得到了以下錯誤:'在子查詢中不能有一個非關係集合元素'我想我可能能夠解決這個問題,一旦我理解SQL更好一點。除非SUBQUERY(成員等)不應該是SUBQUERY(用戶等),除非我錯了。對不起,我缺乏知識。雖然謝謝! – GangstaGraham 2013-03-07 02:21:37

+0

是的,當我使用'users'而不是'members'時,我沒有得到錯誤,所以謝謝!我假設$ member只是一個臨時變量?我是對的@PatrickTescher – GangstaGraham 2013-03-07 02:25:11

+0

啊,是的,用戶不是會員。 $成員只是暫時的。 – 2013-03-07 02:30:56