日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

tableview的reloadData 产生的问题

發布時間:2024/4/13 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 tableview的reloadData 产生的问题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

相信很多人會遇到這種情況,當tableView正在滾動的時候,如果reloadData,偶爾發生App crash的情況。 這種情況有時候有,有時候沒有,已經難倒了很多人。直至今天,我在stackoverflow上面,仍沒有發現真正有說到其本質的帖子。我的處女貼,選擇 這個問題來闡述一下我的觀點。
小弟我英語很好,一般都是用英語記筆記,當然,我知道,論壇憤青很多,如果只貼英文出來,肯定找罵。 故簡單翻譯一下,以顯示我的誠意。 原英文筆記附在后面。 請大家不要挑英語語法錯誤了,筆記就是筆記,不是出書。?


第 一句話,闡述問題的本質:在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 . tableView performSelectorOnMainThread:@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];
});

轉載于:https://my.oschina.net/u/1782374/blog/406859

總結

以上是生活随笔為你收集整理的tableview的reloadData 产生的问题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。