最近偶然發(fā)現(xiàn)項(xiàng)目在 ios 8 上面很容易就崩潰了破衔,最終跟蹤了一下代碼塔嬉,發(fā)現(xiàn)問題出在發(fā)送通知上,一發(fā)送就崩潰了的榛,出現(xiàn)了垃圾地址俗稱的僵尸對象琼了,
image.png
BAD_ACCESS 發(fā)送通知掛掉,雖然傳的值都沒問題夫晌,但是通知的接收對象已經(jīng)銷毀了雕薪,所以會崩潰
image.png
如果在iOS9或更高系統(tǒng)運(yùn)行下是沒問題的,系統(tǒng)做了一些優(yōu)化處理晓淀;
但是在iOS8 系統(tǒng)下就會發(fā)生崩潰 ,當(dāng)你重復(fù)執(zhí)行發(fā)送通知的時(shí)候就會崩潰,其實(shí)就是因?yàn)闆]有移除通知
在接受通知的類或者界面里的 dealloc 方法中 移除通知就OK
/** IOS 8 發(fā)送通知崩潰的問題 */
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
Using iOS 9 or later the Foundation framework release notes contain some good news:
In OS X 10.11 and iOS 9.0 NSNotificationCenter and NSDistributedNotificationCenter will no longer send notifications to registered observers that may be deallocated.
使用iOS 9或更高版本的Foundation框架發(fā)行說明包含一些好消息:
在OS X 10.11和iOS 9.0中所袁,NSNotificationCenter和NSDistributedNotificationCenter將不再向可能已解除分配的已注冊觀察者發(fā)送通知。
這下明白了吧P钻8傺!