NSPredicate problem with fetchedResultsController

Posted by RyJ on Stack Overflow See other posts from Stack Overflow or by RyJ
Published on 2010-03-16T21:20:23Z Indexed on 2010/03/16 21:21 UTC
Read the original article Hit count: 517

Filed under:
|

Please help! I've been trying to figure this out for way too long.

I can't seem to use an NSPredicate in my fetchedResultsController method:

- (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; }

Yet, in another method, where I simply check to see if an object exists, the predicate works like a charm:

- (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;
}}

What am I doing wrong? Thanks in advance!

© Stack Overflow or respective owner

Related posts about core-data

Related posts about nspredicate