我遇到了Core Data的一個奇怪問題。 問題是我正在尋找屬性小於X的對象,並且它沒有返回所有匹配的值。 沒有緩存,我沒有使用// [fetchRequest setFetchBatchSize:50];使用NSPredicate的Fetchrequest不能返回正確的值
NSPredicate *pred = [NSPredicate predicateWithFormat:@"self.level <= [cd] %@", [self.filters objectForKey:@"level"]];
我將其添加到謂詞的MutableArray後來我執行 NSPredicate *比如說myPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:subPredicates];
這是一個函數,返回myPredicate,名爲preparePredicates。目前沒有更多的謂詞,只有一個。 它的NSLog返回:級別< = [cd]「30」。 我已經嘗試過也與串並%的的intValue我 主要功能:
NSError* error;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"myEntity"
inManagedObjectContext:self.context];
[fetchRequest setEntity:entity];
NSPredicate* predicate = [self preparePredicates];
[fetchRequest setPredicate:predicate];
//[fetchRequest setFetchBatchSize:50];
NSArray *fetchedObjects = [self.context executeFetchRequest:fetchRequest error:&error];
NSLog (@"Error: %@", error);
NSLog(@"los fetchedobjects: %i",[fetchedObjects count]);
return fetchedObjects;
它不返回任何錯誤。 如果我看結果沒有一個級別爲6,但有其他匹配(所有都小於指定的)。我知道有一個級別爲6. 如果我在SQLite數據庫瀏覽器中打開.sqlite,我可以在那裏看到它。 如果我做這個程序的
SELECT * FROM zmyentity WHERE zlevel <30;
它不會出現,相同核心數據做。但如果我做了 SELECT * FROM zmyentity WHERE zlevel == 6;它出現了 。 我真的不知道發生了什麼事。也許我正在犯新人的錯誤,所以請給我指出正確的方向。 非常感謝您提前。
非常感謝你。事實上,這是問題的根源。 另外,當使用NSNumber時,predicateWithFormat必須與對象引用%@正常工作,而不是雙重。 NSPredicate * pred = [NSPredicate predicateWithFormat:@「self.level <=%@」,[self.filters objectForKey:@「level」]]; –
@CarlosPastor:不客氣。如果答案有幫助,不要忘記通過點擊勾號大綱來「接受」它,謝謝。 –
@CarlosPastor:你可以這樣做:'%@'用'NSNumber'對象,或'%d'用整數參數,都可以。 - 我用'[[self.filters objectForKey:@「level」] intValue]'參數提出了'%d',因爲我不知道「level」是數字還是字符串。 –