views:

486

answers:

2

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!

A: 

Assuming your "column" attribute is a number, you should compare it to an NSNumber object like this:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"column == %@",[NSNumber numberWithInteger:0]];
gerry3
I tried that. Didn't work.
RyJ
It's not so much that this particular predicate isn't working (the column predicate)... I simply can't seem to get any predicate to work.
RyJ
If I follow my performFetch with this:NSLog(@"filteredArrayUsingPredicate = %@", [fetchedResultsController.fetchedObjects filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"column == %@", [NSNumber numberWithInteger:0]]]);The right objects are returned, but that doesn't help me much because the wrong objects (column != 0) are still in the fetchedResultsController.
RyJ
Have you tried creating a fresh database? With the predicate that I have suggested, your fetched results controller should work.
gerry3
Deleted the database. Nothing's working. I'm just going to have to go a different route. Thanks for helping though.
RyJ
A: 

Since column is a property on the entity then your predicate logic is fine. What results are you seeing that indicate the predicate is not working?

Marcus S. Zarra