2014-03-05 30 views
0

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

- (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]; 
    } 
} 
+0

numberOfRows的外观如何? – nhgrif

+0

你应该在主线程中调用它,而不是在背景上。正如@andrew建议的那样。 – Pawan

回答

0

问题是,我是排空代码,如果表被重新装入,然后[chatData objectAtIndex:row]会导致应用程序崩溃后chatData地方。

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

我想这是做在后台线程的工作?

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

像这样:

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

不,这是解析SDK,查询在后台执行,并在主线程中返回块 – sahara108

+0

由于sahara108表示块中的代码仅在主线程上运行。即使我试过你的代码。但它仍然崩溃。 – Geek

相关问题