1

通常,如果我重新加载表,它不会崩溃。但是,当我在后台获取一些数据然后重新加载表格以显示该数据时,同时如果用户正在滚动表格,则应用程序崩溃。原因是对象数组chatData为空。我不明白它是怎么空的。因为就在重新加载表之前,我将对象设置为chatData. 请注意,仅当用户同时滚动时才会崩溃。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Here app crashes when chatData is empty. Don't get why it is ever empty, because reloadData is called only after setting objects.
    if ([user.userId isEqualToString:[[chatData objectAtIndex:row] objectForKey:SET_SENDER]])
    {

    }
}

- (void)refreshTable
{
    .
    .
    .
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
    {
        self.chatData = [objects mutableCopy];
        [chatTable reloadData];
    }
}
4

2 回答 2

3
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
    {
        self.chatData = [objects mutableCopy];
        [chatTable reloadData];
    }];

我认为这是在后台线程上工作的?

如果是这样,您应该将 chatData 和 reloadData 调用的分配移动到主线程,使用dispatch_async, 因为任何 UI 调用和 UI 接触的任何数据都应该在主线程上完成和分配。

像这样:

[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            self.chatData = [objects mutableCopy];
            [chatTable reloadData];
        });            
    }];
于 2014-03-05T17:54:30.243 回答
0

问题是我清空了chatData代码中的某处,之后如果重新加载表,[chatData objectAtIndex:row]则会导致应用程序崩溃。

于 2014-03-09T07:42:19.543 回答