2014-03-05 33 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

相關問題