通常情况下,如果我重新加载表,它就不会崩溃。但是当我在后台得到一些数据,然后重新加载表来显示这些数据时,同时如果用户正在滚动该表,则app会崩溃。原因是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];
}
}发布于 2014-03-09 07:42:19
问题是,我在代码中的某个地方清空了chatData,然后如果重新加载表,那么[chatData objectAtIndex:row]将导致应用程序崩溃。
发布于 2014-03-05 17:54:30
[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];
});
}];https://stackoverflow.com/questions/22205310
复制相似问题