简体   繁体   English

重新加载tableView时应用程序崩溃

[英]App crashes while reloading tableView

Normally if I reload the table it does not crash. 通常,如果我重新加载表,它不会崩溃。 But when I get some data in background and then reload table to display that data and at same time if user is scrolling the table then app crashes. 但是当我在后台获取一些数据然后重新加载表来显示该数据时,如果用户正在滚动表,那么应用程序崩溃。 The reason is array of objects chatData is empty. 原因是对象数组chatData为空。 I don't get how it is empty. 我不知道它是如何空洞的。 Because just before reloading the table I set object into chatData . 因为在重新加载表之前我将对象设置为chatData Note that it crashes only if user is scrolling at the same time. 请注意,仅当用户同时滚动时才会崩溃。

- (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];
    }
}
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
    {
        self.chatData = [objects mutableCopy];
        [chatTable reloadData];
    }];

I assume this is doing its work on a background thread? 我假设这是在后台线程上做它的工作?

If so, you should move the assignment of the chatData and the reloadData call to the main thread, using dispatch_async , as any UI calls and any data that the UI touches should be done and assigned on the main thread. 如果是这样,您应该使用dispatch_async将chatData和reloadData调用的分配移动到主线程,因为任何UI调用都应该在主线程上完成并分配UI接触的任何数据。

Like so: 像这样:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM