Record a crash caused by modifying the data source while an iOS UITableView is refreshing

First look at the crash stack information
Please add image description

Since tableview calls layoutsubViews, it executes the proxy method-
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

Please add image description
Since the crash occurred in the system method, we cannot directly see which method was called that caused the crash.


After many debuggings, I found that the layoutsubviews of the tableView were triggered by calling reloadData , and then executed to the proxy method.

At this time, I have questions again, because my crash here is after re-requesting.
This is the call after the request is successful, and the data source is cleared (the re-requested data is empty)
Please add image description

Here is how to re-request
Please add image description

As you can see from the picture above, I refreshed the tableView before performing the re-request.

There is a speculation at this time that we have performed a refresh. At this time, the refresh still uses the old data, but the refresh is an asynchronous process. At this time, if the request result returns quickly, we have performed a clear data source, but the refresh has not yet been completed. , the ongoing refresh will not be able to obtain the data, resulting in a crash.

The solution is to modify the data source and wait until the refresh is completed,
so we add a delay

  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.dataList removeAllObjects];
        [self.heightList removeAllObjects];
        NSLog(@"哈哈哈嘻乖数据修改数据");
        [self.dataList addObjectsFromArray:refreshDataList];
        [self.heightList addObjectsFromArray:heihgtList];
        
        [self.tableView reloadData];
    });
  
    

Guess you like

Origin blog.csdn.net/LIUXIAOXIAOBO/article/details/133361683