开发者

Predicate Is Not Filtering Through Data Properly

I am trying to display the count of objects from my fetch controller in my cell's badgeString.

For some reason, the badge is staying the same for all cells, and not adjusting for each.

Indexpath.row ==2 is for last week and Indexpath.row == 3 is for l开发者_StackOverflowast month. In my cell configuration, I have:

if (indexPath.row > 1)
{
    cell.badgeString = [NSString stringWithFormat:@"%i", [[self.fetchedResultsController fetchedObjects]count]];
}

This is my fetch controller method:

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

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0] ;
NSDate *today = [NSDate date]; 
NSDate *thisWeek  = [today dateByAddingTimeInterval: -604800.0];
NSDate *thisMonth = [today dateByAddingTimeInterval: -2629743.83]; // Use NSCalendar for

if (indexPath.row ==2)
{
    [fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"(date >= %@) AND (date <= %@)", thisWeek, today]];
}
else if (indexPath.row ==3)
{
    [fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"(date >= %@) AND (date <= %@)", thisMonth, today]];
}

Full Fetch Controller Code:

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

    // Create the fetch request for the entity.
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Session" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0] ;
    NSDate *today = [NSDate date]; 
    NSDate *thisWeek  = [today dateByAddingTimeInterval: -604800.0];
    NSDate *thisMonth = [today dateByAddingTimeInterval: -2629743.83]; // Use NSCalendar for

    if (indexPath.row ==2)
    {
        [fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"(date >= %@) AND (date <= %@)", thisWeek, today]];
    }
    else if (indexPath.row ==3)
    {
        [fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"(date >= %@) AND (date <= %@)", thisMonth, today]];
    }

    // Set the batch size to a suitable number.
    [fetchRequest setFetchBatchSize:20];

    // Edit the sort key as appropriate.
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

    [fetchRequest setSortDescriptors:sortDescriptors];

    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

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

    NSError *error = nil;
    if (![self.fetchedResultsController performFetch:&error])
    {
        /*
         Replace this implementation with code to handle the error appropriately.

         abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
         */
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }
    NSLog(@"Number of Objects = %i",
           [[__fetchedResultsController fetchedObjects] count]);
    return __fetchedResultsController;
    NSLog(@"Number of Objects = %i",
          [[__fetchedResultsController fetchedObjects] count]);

}

Code For Cell Configuration:

- (void)configureCell:(TDBadgedCell *)cell atIndexPath:(NSIndexPath *)indexPath
    {
    if (indexPath.row > 1)
        {
            cell.badgeString = [NSString stringWithFormat:@"%i", [[self.fetchedResultsController fetchedObjects]count]];
        }


- (void) refreshTableData {
NSMutableArray *results = [NSMutableArray arrayWithCapacity:2];
...
NSArray *fetchResults = [first predicate call goes here];
[results addObject:fetchResults];
fetchResults = [second predicate call goes here];
[results addObject:fetchResults];
...
[tableview reloadData];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜