文森說技術

iOS, Web Development Notes
- , ,

在 iOS 中避免 WebThreadLock 造成 Crash

目錄

  1. 解法

最近的專案中,需要在 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
2
3
4
5

bool _WebTryThreadLock(bool), 0x115504830: Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now...
1 0x108412aa8 WebThreadLock
2 0x102579e77 -[UIWebView reload]

解法

強制讓 [webView reload] 一定要在 main thread 即可解決, call method performSelectorOnMainThread:withObject:waitUntilDone: 指定要在 main thread 上執行的 method:

1
[webView performSelectorOnMainThread:@selector(reload) withObject:nil waitUntilDone:NO];
如果覺得這篇對你有幫助,歡迎幫忙分享給其他人 😀