2010-03-16 78 views
1

請幫忙!我一直試圖弄清楚這一點太長了。fetchedResultsController的NSPredicate問題

我似乎無法在我的fetchedResultsController方法使用一個NSPredicate:

- (NSFetchedResultsController *)fetchedResultsController {  
if (fetchedResultsController != nil) return fetchedResultsController; 

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Tweet" inManagedObjectContext:managedObjectContext]; 
[fetchRequest setEntity:entity]; 

NSPredicate *predicate = [NSPredicate predicateWithFormat: @"column == 0"]; 
[fetchRequest setPredicate:predicate]; 

[fetchRequest setReturnsObjectsAsFaults:NO]; 
[fetchRequest setFetchBatchSize:20]; 

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"created_at" ascending:NO]; 
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil]; 
[fetchRequest setSortDescriptors:sortDescriptors]; 

NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"]; 
aFetchedResultsController.delegate = self; 
self.fetchedResultsController = aFetchedResultsController; 

[aFetchedResultsController release]; 
[fetchRequest release]; 
[sortDescriptor release]; 
[sortDescriptors release]; 

return fetchedResultsController; } 

然而,在另一種方法,在這裏我簡單的檢查,看看如果一個對象存在,謂語就像一個魅力:

- (BOOL)findObjectWithKey:(NSString *)key andValue:(NSString *)value sortBy:(NSString *)sort { 
NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Tweet" inManagedObjectContext:managedObjectContext]; 
[request setEntity:entity]; 

NSPredicate *predicate = [NSPredicate predicateWithFormat: @"%K == %@", key, value]; 
[request setPredicate:predicate]; 

[request setReturnsObjectsAsFaults:NO]; 

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:sort ascending:YES]; 
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil]; 
[request setSortDescriptors:sortDescriptors]; 

NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"]; 
aFetchedResultsController.delegate = self; 

NSError *error = nil; 
NSArray *result = [managedObjectContext executeFetchRequest:request error:&error]; 

[aFetchedResultsController release]; 
[request release]; 
[sortDescriptor release]; 
[sortDescriptors release]; 

if ((result != nil) && ([result count]) && (error == nil)) { 
    return TRUE; 
} else { 
    return FALSE; 
}} 

我在做什麼錯?提前致謝!

+1

什麼是列? – 2010-03-16 21:43:16

+0

「列」是核心數據屬性的關鍵。 – RyJ 2010-03-16 21:45:38

回答

0

假設你的「列」屬性是一個數字,你應該把它比作一個NSNumber對象是這樣的:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"column == %@",[NSNumber numberWithInteger:0]]; 
+0

我試過了。沒有工作。 – RyJ 2010-03-16 21:51:28

+0

這並不是說這個特殊的謂詞不起作用(列謂詞)......我似乎無法得到任何謂詞。 – RyJ 2010-03-16 21:54:55

+0

如果我按照我的performFetch這個: NSLog(@「filteredArrayUsingPredicate =%@」,[fetchedResultsController.fetchedObjects filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@「column ==%@」,[NSNumber numberWithInteger:0]]]); 返回正確的對象,但這並沒有多大幫助,因爲錯誤的對象(列!= 0)仍然在fetchedResultsController中。 – RyJ 2010-03-16 21:59:41

0

由於列是實體的屬性,然後你的謂詞邏輯是罰款。你看到哪些結果表明謂詞不起作用?