I have a UITableView on one of my view controllers, it is updated with log messages from multiple parts of my app, asynchronously. It worked fine, but today I noticed a weird bug. After about 2 hours, the entire tableview turned blank. there are no cells, no line separators, just the background color.
There are only 2 entry paths into this tableview.
NSMutableArray* tableViewCellData;
//in the init method:
tableViewCellData = [[NSMutableArray alloc]initWithCapacity:15];
-(void)setContextActionWithTitle:(NSString *)title description:(NSString *)description
ContextConsoleLogItem* temp = [[ContextConsoleLogItem alloc] initWithDate:[NSDate date] title:title message:description contextAction:kNoContextAction];
[tableViewCellData insertObject:temp atIndex:0];
[contextActionTableView reloadData];
}
//pretty standard data source management
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return kNumberOfSections;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [tableViewCellData count];
}
//here's how the cell displays itself.
#pragma mark -
#pragma mark Cell customization
-(void)configureCell:(UITableViewCell*) cell atIndexPath:(NSIndexPath*) indexPath
{
ContextConsoleLogItem* logItem = [tableViewCellData objectAtIndex:indexPath.row];
cell.backgroundColor = [UIColor blackColor];
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.font = [UIFont systemFontOfSize:9];
cell.detailTextLabel.textColor = [UIColor whiteColor];
cell.textLabel.text = logItem.message;
cell.textLabel.numberOfLines =3;
}
What could be causing the tableview to “lose” all of it’s data and stop responding to
-(void)setContextActionWithTitle:(NSString *)title description:(NSString *)description
One thing that I suspect is that the NSMutableArray was allocated with insufficient capacity. It would reach maximum capacity pretty quickly. Some of the messages posted using the method above are coming from calls to performSelectorInBackground . Could it be that one of my background selectors hits the capacity of the NSMutableArray and fails to re-allocate it? But then again, even an empty tableview should still have cells, so I should be able to see line separators.
Should I wrap my calls to setContextActionWithTitle:description: with performSelectorOnMainThread ?
Could it be that 2 separate calls to update the tableview have been made and somehow they left the table in an inconsistent state? (I’m not getting any exceptions here)
This is a very puzzling behavior, and I’d appreciate any help in debugging it!
Never touch anything in UIKit from a background thread. Ever.