从一个api请求加载数据并将其存储在一个数组中(假设n个对象进入响应json对象),另一个api请求从第一个api请求中获取参数并加载n个对象的状态。
1)第一个api请求将加载n个对象并将其显示在表中:
dispatch_queue_t loadDataQueue = dispatch_queue_create("loadDataQueue",NULL);
dispatch_async(loadDataQueue, ^{
// Perform long running process
[self loadData];
dispatch_async(dispatch_get_main_queue(), ^{
// Update the UI
[tableView reloadData];
[self hideActivityView];
});
});
2)现在调用loadstatus
方法,它从objectatindex
中获取参数并加载objectatindex
的状态数据。因此,这个方法在cellForRowAtIndexPath
方法中调用n次。
dispatch_queue_t loadStatusQueue = dispatch_queue_create("loadStatusQueue",NULL);
dispatch_async(loadStatusQueue, ^{
// Perform long running process
[self loadStatus];
dispatch_async(dispatch_get_main_queue(), ^
// Update the UI
[tableView reloadData];
});
});
它一次更新一行。所以重装表格n次。加载所有对象的状态需要时间。出现了一些吊死问题。
有人能为这个或其他方法提供有效的解决方案吗?
发布于 2015-09-11 16:57:13
你问题中的信息有点不清楚(特别是关于objectAtIndex
.这是否意味着在cellForRowAtIndexPath
中您获得了单元格的信息,然后根据它发送了另一个异步请求来获取它的状态?)
这可能不足以说明所有的原因可能会使您的应用程序缓慢,但我可以说,这不是一个好主意,重新加载整个表,只更新一个单元格。另外,我认为您应该先调用loadData
来获取基本信息的完整数据列表,然后调用loadStatus
表示“可见”单元格。
我想您知道如何将加载的数据/状态存储在数组中,以防止重取数据。因此,下面的示例可能是您可以采用的改进性能的方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"ReusableCell" forIndexPath:indexPath];
[cell configureData:self.loadedData[indexPath.row]];
if (self.loadedStatus[indexPath.row]) {
// If the status has been loaded then
[cell configureStatus:self.loadedStatus[indexPath.row]];
} else {
dispatch_queue_t loadStatusQueue = dispatch_queue_create("loadStatusQueue",NULL);
__weak __typeof(self) weakSelf = self;
dispatch_async(loadStatusQueue, ^{
__strong __typeof(weakSelf) strongSelf = weakSelf;
if (!strongSelf) {
return;
}
// Perform your long running process here
// Eg: [strongSelf loadStatusForIndex:indexPath.row];
UITableViewCell *blockCell = (UITableViewCell *)[strongSelf.tableView cellForRowAtIndexPath:indexPath];
dispatch_async(dispatch_get_main_queue(), ^{
[strongSelf.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
});
}
});
....
}
return cell;
}
https://stackoverflow.com/questions/32524660
复制相似问题