user4951
user4951

Reputation: 33090

how to limit number of objects in NSFetchResultsController?

I have NSFetchedResultsController that I need to show data at tableView

I use NSFetchedResultsControllerDelegate

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
    DLog(@"controllerWillChangeContent: %@", controller);
    [self.tableViewA beginUpdates];

}


- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
           atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {

    DLog(@"didChangeSection: %@", controller);

    switch(type) {
        case NSFetchedResultsChangeInsert:
            [self.tableViewA insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [self.tableViewA deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}


- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
       atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
      newIndexPath:(NSIndexPath *)newIndexPath {

    DLog(@"controller:%@\ndidChangeObject:%@\natIndexPath:%@\nforChangeType:%d\nnewIndexPath:%@\n",controller, anObject, indexPath, type, newIndexPath);

    UITableView *tableView = self.tableViewA;
    DLog(@"%@", self.tableViewA);
    switch(type) {
        case NSFetchedResultsChangeInsert:
                [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationRight];
            break;

        case NSFetchedResultsChangeDelete:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
            break;

        case NSFetchedResultsChangeUpdate:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationNone];
            //[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
            break;

        case NSFetchedResultsChangeMove:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]withRowAnimation:UITableViewRowAnimationRight];
            break;
    }

}


- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
    [self.tableViewA endUpdates];
}

and before performFetch I use

 request.fetchLimit = 100;
    request.fetchBatchSize = 100;

that's mean I want to show data just 100, but when I do something that will make NSManagedObjectContext was changed, it will call NSFetchedResultsControllerDelegate and will insert any data to tableView. the problem is, the tableView can show more than 100 data..

I use this function to set numberOfRowsInSection

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.FetchController sections] objectAtIndex:section];
    CalledRowCountingNotYetCallRowForSection=true;
    [self.tableViewA setBounces:YES];


    if([sectionInfo numberOfObjects]>100){
        //[Timer searchCriteriaChanged];
        CLog(@"Something Wrong This NumberOfRow: %d", [sectionInfo numberOfObjects]);
    }
    else{
    }
    return [sectionInfo numberOfObjects];
}

how can I make tableView can't show more than 100 data?

Upvotes: 1

Views: 463

Answers (1)

user1226119
user1226119

Reputation: 324

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

This function is NOT call to set numberOfRowsInSection but to GET the number.

-(void)controller:(NSFetchedResultsController *)controller 
       didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
       atIndex:(NSUInteger)sectionIndex 
       forChangeType:(NSFetchedResultsChangeType)type {

DLog(@"didChangeSection: %@", controller);

switch(type) {
    case NSFetchedResultsChangeInsert:
   if([YourtableView numberOfRowsInSection:yourSection]>100){
      return;  //TO avoid insertion 
   }
   ...
   ...
   ...
 }
}

Upvotes: 1

Related Questions