簡體   English   中英

iPhone上的CoreData是否支持IN謂詞?

[英]Does CoreData on iPhone support IN predicates?

我正在嘗試根據用戶定義的類型列表獲取一堆特定類型的記錄...

[fetchRequest setEntity:[NSEntityDescription entityForName:@"myRecord" inManagedObjectContext:self.managedObjectContext]];  
NSSet   *shipTypes = [NSSet setWithObjects:[NSNumber numberWithInt:70],
                    [NSNumber numberWithInt:71],
                    [NSNumber numberWithInt:72],
                    [NSNumber numberWithInt:73],
                    [NSNumber numberWithInt:74],
                     nil];
NSPredicate *aPredicate = [NSPredicate predicateWithFormat:@"type in %@", shipTypes];
[fetchRequest setPredicate:aPredicate];
theRecords = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];

...運行時,executeFetchRequest消息拋出異常......

 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'unimplemented SQL generation for predicate : (type IN {71, 73, 70, 72, 74})'

我做錯了什么,或者這真的不受支持?

我相信Alex是正確的,你必須使用NSArray,雖然顯然它更好,如果在這里接受NSSet,因為順序並不重要(盡管它可能會影響SQL的運行速度)。

作為旁注,我從不在任何代碼中使用+ predicateWithFormat:調用,因為它無法進行編譯時的健全性或類型檢查。 我強烈建議使用個別課程。

在這種情況下,我會這樣做:

fetchRequest.entity = [NSEntityDescription entityForName:@"myRecord" inManagedObjectContext:self.managedObjectContext]];

NSArray *shipTypes = [NSArray arrayWithObjects:[NSNumber numberWithInt:70],
                                        [NSNumber numberWithInt:71],
                                        [NSNumber numberWithInt:72],
                                        [NSNumber numberWithInt:73],
                                        [NSNumber numberWithInt:74],
                                         nil];
fetchRequest.predicate = [NSComparisonPredicate predicateWithLeftExpression:[NSExpression expressionForKeyPath:@"type"] rightExpression:[NSExpression expressionForConstantValue:shipTypes] modifier:NSDirectPredicateModifier type:NSInPredicateOperatorType options:0];

theRecords = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];

並不是說這會在編譯時捕獲到這個特定的錯誤,但它可能會在NSExpression級別捕獲它,從而使錯誤更清楚。

嗯。 我嘗試添加ANY關鍵字並使用NSArray。 實際上,當我從Core Data獲得此異常時,我正在使用NSAArray。 回想起來,因為我將兩個表達式與AND組合在一起,這對Core Data來說是一個問題。 (我知道我應該回去驗證這一點,但這個想法剛剛發生在我看到這篇文章時。)這是我的原始表達:

NSPredicate* predicate = [NSPredicate predicateWithFormat:@"BPM BETWEEN { %d, %d } AND GENRE IN %@", lower, upper, genres];

我的解決方案是將其分為兩部分。 現在,我使用謂詞發出BPM的查詢,但是我使用結果數組並使用-filteredArrayUsingPredicate和@“genre IN%@”。 從而:

array = [array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"genre IN %@", genres]];

這適合我。

我為此苦苦掙扎。 最終我最終為每個集合做了一個子謂詞。

例如:

NSMutableArray *subpredicates = [NSMutableArray array];

NSArray *shipTypes = [NSArray arrayWithObjects:[NSNumber numberWithInt:70],
                                    [NSNumber numberWithInt:71],
                                    [NSNumber numberWithInt:72],
                                    [NSNumber numberWithInt:73],
                                    [NSNumber numberWithInt:74],
                                     nil];

for (NSNumber *num in shipTypes) {
    [subpredicates addObject:[NSPredicate predicateWithFormat:@"ANY type == %@", num]];         
}

NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:subpredicates];

這是假設類型是NSSet。 如果不是你可以只有:

for (NSNumber *num in shipTypes) {
    [subpredicates addObject:[NSPredicate predicateWithFormat:@"type == %@", num]];         
}

嘗試將謂詞更改為@"ANY type IN %@"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM