2011-02-24 31 views
1

我想創建一個fetchRequest顯示我已經不在另一個數組的所有值。此代碼返回我期望的數組:謂詞適用於數組,但不是fetchRequest

NSArray *questionChoices = [self.currentQuestionChoiceGroup.questionChoices allObjects]; 
NSArray *filteredQuestionChoices = [questionChoices filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"NONE %@ == code", myarr]]; 

myarr包含1個項目,並且1個項目從篩選結果中排除,如預期的那樣。但是,此代碼不工作:

NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
request.entity = [NSEntityDescription entityForName:@"QuestionChoice" inManagedObjectContext:self.managedObjectContext]; 
request.predicate = [NSPredicate predicateWithFormat:@"NONE %@ == code AND questionChoiceGroup == %@", myarr, self.currentQuestionChoiceGroup]; 

當我執行這個讀取請求時,我得到屬於當前questionChoiceGroup所有questionChoices,包括有在myArr,該代碼的人。這似乎完全忽略和語句的第一部分。

我看不出兩者之間應該導致不同結果的任何區別。誰能幫忙?

編輯簡單的解釋:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"questionChoiceGroup == %@ AND NONE %@ == code", self.currentQuestionChoiceGroup, [NSArray arrayWithObject:@"A"]]; 

NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"displayOrder" ascending:YES]]; 
request.entity = [NSEntityDescription entityForName:@"QuestionChoice" inManagedObjectContext:self.managedObjectContext]; 

request.predicate = predicate; 
NSArray *filteredQuestionChoices1 = [self.managedObjectContext executeFetchRequest:request error:nil]; 
NSLog(@"my filtered question choices 1: %@", [filteredQuestionChoices1 valueForKey:@"code"]); 

NSArray *filteredQuestionChoices2 = [filteredQuestionChoices1 filteredArrayUsingPredicate:predicate]; 
NSLog(@"my filtered question choices 2: %@", [filteredQuestionChoices2 valueForKey:@"code"]); 

filteredQuestionChoices1包含 「A」 的代碼的項目。 filteredQuestionChoices2沒有該項目。第二個謂語如何過濾掉任何東西,第一個謂語沒有過濾掉?這兩個語句使用完全相同的斷言。爲什麼我從fetchedRequest獲取對象,如果在數組上使用謂詞時,完全相同的謂詞會將這些對象過濾掉?

回答

1

核心數據手冊退房Fetch Predicates and Sort Descriptors

如果您使用的是基於SQL的數據存儲(例如,SQLite的),那麼謂語轉換爲SQL和數據庫調用的,而不是由可可。所以你可以得到不同於你對標準數組看到的行爲,並且對支持的內容也有限制。具體來說,有上ALLANYIN操作(和你的NONE運營商的NOT(ANY ...)等效)的限制。

看起來你試圖對你提供的數組作爲參數進行NONE操作,而不是從數據庫中提取數據。我敢打賭,這是SQL翻譯的謂詞絆腳石。

0

如果使用AND的語句有兩個部分,請將每個部分包裝在括號中。另外,在比較動態屬性名稱時,%K是適當的格式說明符。

request.predicate = [NSPredicate predicateWithFormat:@"(NONE %K == code) AND (questionChoiceGroup == %@"), myarr, self.currentQuestionChoiceGroup]; 

從蘋果公司的謂詞編程指南的例子:

可可支持廣泛的類型謂詞的 ,包括以下內容:

  • 簡單的比較,如等級= = 7 或名字如'Mark'

  • 個案例或 音調符號不敏感的查找,如 名稱包含苯並[cd] '雪鐵龍'

  • 邏輯 操作,如(名字 beginswith 'M')AND(lastName的像 '阿德利')

+0

只是去嘗試了;它不會改變任何東西......儘管它確實使代碼變得更清晰。 – GendoIkari 2011-03-01 22:55:25

+0

使用%K作爲動態屬性名稱的格式說明符。我已更新我的示例。 – 2011-03-01 22:58:23

+0

我不是一個比較動態屬性名稱...'NONE%@ == code'是爲了消除具有由%@通過在陣列中的代碼的所有項目... – GendoIkari 2011-03-02 17:36:57

4

我想嘗試重新操作你的謂詞。相反的:

[NSPredicate predicateWithFormat:@"NONE %@ == code", myarr] 

嘗試:

[NSPredicate predicateWithFormat:@"NOT(code IN %@)", myarr] 

我認爲後者可能會更容易一點的CoreData轉化爲相應的SQL條件。

+0

其實,這也正是我結束做起來。 '不代碼IN'工作得很好。但是,如果它有時可以用不同於數組的方式處理謂詞,那麼顯然有些內容我不瞭解核心數據。我試圖理解,以避免未來的問題。另外,最好知道'NONE'和'NOT IN'是否完全相同。 – GendoIkari 2011-03-04 19:13:09

相關問題