UITableView reloadData的正確方法。
相信很多人會遇到這種情況,當tableView正在滾動的時候,如果reloadData,偶爾發生App crash的情況。
在tableView的dataSource被改變 和 tableView的reloadData被調用之間有個時間差,而正是在這個期間,tableView的delegate方法被調用,如果新的dataSource的count小于原來的dataSource count,crash就很有可能發生了。
下面的筆記提供了兩種解決方案,和記錄了一個典型的錯誤,即 在background thread中修改了datasource,雖然調用 [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nilwaitUntilDone:NO];
記住正確的原則: Always change the dataSource and(注意這個and) reloadData in the mainThread. What's more, reloadData should be called immediately after the dataSource change.
If dataSource is changed but tableView's reloadData method is not called immediately, the tableView may crash if it's in scrolling.
Crash Reason: There is still a time gap between the dataSource change and reloadData. If the table is scrolling during the time gap, the app may Crash!!!!
WRONG WAY:
Following codes is WRONG: even the reloadData is called in main thread, there is still a time gap between the dataSource change and reloadData. If the table is scrolling during the time gap, the app may Crash!!!!
wrong codes samples:
-(void) changeDatasource_backgroundThread
{
@autoreleasepool{
[self.dataSourceArray removeAllObjects];
[self.tableViewperformSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
}
}
RIGHT WAY:
Principle: Always change dataSource in MAIN thread and call the reloadData immediately after it.
Option 1: If the operation to change the dataSource should be executed in background, the operation can create a temp dataSource array and pass it to main thread with notification, the main thread observes the notification, assign the tmpDataSource to dataSource and reload the tableView by reloadData.
Option 2: In the background, call the GDC dispatch_async to send the two methods to main thread together.
dispatch_async(dispatch_get_main_queue(), ^{
self.dataSourceArray= a new Array.
[self.tableView reloadData];
});