iPhone4 iOS5从后台选择器刷新UITableView是否安全?



我在我的一个视图控制器上有一个UITableView,它是用我的应用程序的多个部分的日志消息异步更新的。它工作得很好,但今天我注意到一个奇怪的错误。大约2小时后,整个tableview变成空白。没有单元格,没有分隔线,只有背景颜色。

这个tableview只有2个入口路径

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

是什么导致tableview"丢失"所有数据并停止响应

-(void)setContextActionWithTitle:(NSString *)title description:(NSString *)description

我怀疑的一件事是NSMutableArray分配的容量不足。它会很快达到最大容量。使用上述方法发布的一些消息来自对performSelectorInBackground的调用。会不会是我的一个后台选择器击中了NSMutableArray的容量并且无法重新分配它?不过,即使是空的tableview也应该有单元格,所以我应该能看到行分隔符。

我应该用performSelectorOnMainThread包装我的呼叫setContextActionWithTitle:description:吗?

可能是2个单独的调用更新tableview已经作出,不知怎么的,他们离开了表在一个不一致的状态?(我在这里没有得到任何异常)

这是一个非常令人费解的行为,我非常感谢任何帮助调试它!

永远不要从后台线程触摸UIKit中的任何东西。div。

我怀疑的一件事是NSMutableArray被分配的容量不足

绝对不是这样。如文档中所述:

可变数组根据需要展开;numItems只是建立对象的初始容量。

Jim是正确的,永远不要从后台线程触摸UIKit。

最新更新