在 iOS 中避免 WebThreadLock 造成 Crash
目錄
最近的專案中,需要在 UITableView
中放 UIWebView
,web view 本身會有 web thread lock,因為他在 reload 之後是 UI 的 re-render,會被要求一定要在 main thread 上面跑。
UI 繪製一定要在 main thread 上
1 | [webView reload]; |
但是當 web view 要 reload 時, table view 也正在 reload,web view reload 所使用的 WebThreadLock
就會被推到 secondary thread ,就會導致 crash 而跳出以下的錯誤訊息:
1 |
|
解法
強制讓 [webView reload]
一定要在 main thread 即可解決, call method performSelectorOnMainThread:withObject:waitUntilDone:
指定要在 main thread 上執行的 method:
1 | [webView performSelectorOnMainThread:@selector(reload) withObject:nil waitUntilDone:NO]; |