sdk 中涉及到 UI 操作的時候疫赎,一定要注意線程問題目胡!一定要注意線程問題!一定要注意線程問題!
從最初開始學(xué)習(xí) iOS 的時候姻采,我們就被告知 UI 操作一定要放在主線程進(jìn)行盯串,這是因?yàn)?UIKit 的方法不是線程安全的胞枕,保證線程安全需要極大的開銷羹与。
試想一下幾種場景:
- 兩個線程同時設(shè)置同一個背景圖片,則很有可能因?yàn)楫?dāng)前圖片被釋放了兩次而導(dǎo)致應(yīng)用崩潰爹橱;
- 兩個線程同時設(shè)置同一個 UIView 的背景色萨螺,則很有可能渲染顯示的是顏色 A,而此時在 UIView 邏輯樹上的背景顏色屬性為 B愧驱;
- 兩個線程同時操作 View 的樹形結(jié)構(gòu):在線程 A 中 for 循環(huán)遍歷并操作當(dāng)前 View 的所有 subView慰技,而此時線程 B 中將某個 subView 直接刪除,這就導(dǎo)致了錯亂组砚,甚至導(dǎo)致應(yīng)用崩潰吻商。
出了什么問題?
最近 hybrid sdk 收尾惫确,偶然發(fā)現(xiàn)線上有一類 crash 最近兩個版本穩(wěn)步上升手报,而且可以肯定的是我負(fù)責(zé)的 sdk 提供的 web 容器導(dǎo)致的蚯舱,__ZL17_WebTryThreadLockb 是函數(shù)調(diào)用棧最后調(diào)用的 api改化。
crash 線程調(diào)用棧詳細(xì)如下:
Thread 17 Crashed:
0 WebCore __ZL17_WebTryThreadLockb
1 WebCore _WebThreadLock
2 UIKit -[UIWebBrowserView resignFirstResponder]
3 UIKit -[UIResponder _resignIfContainsFirstResponder]
4 UIKit -[UIView(Hierarchy) _willMoveToWindow:]
5 UIKit -[UIView(Internal) _addSubview:positioned:relativeTo:]
6 UIKit ___53-[_UINavigationParallaxTransition animateTransition:]_block_invoke_2
7 UIKit +[UIView(Animation) performWithoutAnimation:]
8 UIKit ___53-[_UINavigationParallaxTransition animateTransition:]_block_invoke
9 UIKit +[UIView(Internal) _performBlockDelayingTriggeringResponderEvents:]
10 UIKit -[_UINavigationParallaxTransition animateTransition:]
11 UIKit -[UINavigationController _startCustomTransition:]
12 UIKit -[UINavigationController _startDeferredTransitionIfNeeded:]
13 UIKit -[UINavigationController __viewWillLayoutSubviews]
14 UIKit -[UILayoutContainerView layoutSubviews]
15 UIKit -[UIView(CALayerDelegate) layoutSublayersOfLayer:]
16 QuartzCore -[CALayer layoutSublayers]
17 QuartzCore __ZN2CA5Layer16layout_if_neededEPNS_11TransactionE
18 QuartzCore __ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE
19 QuartzCore __ZN2CA7Context18commit_transactionEPNS_11TransactionE
20 QuartzCore __ZN2CA11Transaction6commitEv
21 QuartzCore __ZN2CA11Transaction14release_threadEPv
22 libsystem_pthread.dylib __pthread_tsd_cleanup
23 libsystem_pthread.dylib __pthread_exit
24 libsystem_pthread.dylib __pthread_wqthread
25 libsystem_pthread.dylib _start_wqthread
原因是什么?
Stack Overflow 上的問題和解答貼出兩個大家一起參考:
app-crash-no-idea-why
ios-webtrythreadlock-crash
根本原因:Tried to obtain the web lock from a thread other than the main thread or the web thread.
問題定位
ABAuthorizationStatus authStatus = ABAddressBookGetAuthorizationStatus();
if (authStatus == kABAuthorizationStatusNotDetermined)
{
//獲取授權(quán)
ABAddressBookRef addressBook = ABAddressBookCreate();
ABAddressBookRequestAccessWithCompletion( addressBook, ^(bool granted, CFErrorRef error) {
if (granted)
{
[self openContact];
}
else
{
[self showAlertView];
}
});
}
之前草率的確定為上述代碼問題枉昏,上述代碼確實(shí)存在兩個問題
- 內(nèi)存泄漏
- 非主線程調(diào)用 UI 操作
但重新看 crash 調(diào)用棧陈肛,確定并不會是 AddressBook 導(dǎo)致,而真實(shí)原因已無從考證兄裂。
更新文章句旱,留作后人查閱阳藻。