iOS中如何正確釋放GCD定時(shí)器(dispatch_source_t)
一.現(xiàn)象
通過(guò)云跡的崩潰,查詢到崩潰在福袋的釋放緩存的方法(clearLuckyBagInfo)中,然后就查看這個(gè)釋放緩存的方法,發(fā)現(xiàn)這個(gè)方法中都是簡(jiǎn)單的釋放,也有保護(hù)操作,就很納悶兒為什么崩潰在了這里.
- (void)clearLuckyBagInfo{
self.LuckyBagDetailModel = nil;
self.contentView = nil;
self.residueTime = 0;
if (self.countDownTimer) {
dispatch_cancel(self.countDownTimer);
self.countDownTimer = nil;
}
self.isSuspendTimer = NO;
self.liveDto = nil;
[self.winPriceArray removeAllObjects];
self.winPriceArray = nil;
if (self.tenSecondTimer) {
dispatch_cancel(self.tenSecondTimer);
self.tenSecondTimer = nil;
}
self.IsReciveLotteryMessage = NO;
self.timeGone = 0;
}
二.查詢和驗(yàn)證
查看了上面的釋放方法中,通過(guò)分析只有可能是定時(shí)器釋放時(shí)引起的crash,然后就去查詢dispatch_source_t的crash問(wèn)題,結(jié)果發(fā)現(xiàn)了在dispatch_suspend 狀態(tài)下,不能去釋放定時(shí)器,如果此時(shí)釋放定時(shí)器,就會(huì)crash.
然后查看代碼中為了定時(shí)器的時(shí)間退到后臺(tái)再回來(lái)時(shí)的準(zhǔn)確性,就在退到后臺(tái)中將定時(shí)器掛起(dispatch_suspend ),回到前臺(tái)是發(fā)現(xiàn)福袋未結(jié)束時(shí)就重新開(kāi)啟定時(shí)器(dispatch_resume),如果用戶從后臺(tái)回到前臺(tái)時(shí)福袋已經(jīng)結(jié)束了,這時(shí)就沒(méi)調(diào)用dispatch_resume開(kāi)啟定時(shí)器,這時(shí)候釋放定時(shí)器就會(huì)引起crash.然后就和測(cè)試驗(yàn)證了這個(gè)場(chǎng)景,確實(shí)會(huì)引起crash.
// 程序進(jìn)入后臺(tái)
- (void)resignActive:(NSNotification *)notification {
if ([self.LuckyBagDetailModel.drawStatus isEqualToString:@"0"] && self.countDownTimer) {
[self pauseCountDownTimer];//掛起定時(shí)器
NSDate* currentdate = [NSDate dateWithTimeIntervalSinceNow:0];
NSTimeInterval a=[currentdate timeIntervalSince1970];
[[NSUserDefaults standardUserDefaults] setObject:@(a) forKey:snliveLuckyBagResignActiveDate];
}
}
// 程序進(jìn)入前臺(tái)
- (void)becomeActive:(NSNotification *)notification {
if ([self.LuckyBagDetailModel.drawStatus isEqualToString:@"0"] && self.countDownTimer) {
[self resumeCountDownTimer];//重啟定時(shí)器
NSDate* currentdate = [NSDate dateWithTimeIntervalSinceNow:0];
NSTimeInterval a=[currentdate timeIntervalSince1970];
NSTimeInterval resignDate = [[NSUserDefaults standardUserDefaults] doubleForKey:snliveLuckyBagResignActiveDate];
self.timeGone = a - resignDate;
}else{
self.timeGone = 0;
}
}
三.解決辦法
1.設(shè)置屬性 isSuspendTimer 記錄定時(shí)器timer是否 處于dispatch_suspend的狀態(tài)具温。在定時(shí)器掛起時(shí)設(shè)置為YES,重新啟動(dòng)時(shí)設(shè)置為NO. 只要在 調(diào)用dealloc 時(shí)判斷下浊伙,已經(jīng)調(diào)用過(guò) dispatch_suspend 則再調(diào)用下 dispatch_resume后再cancel途样,然后再釋放timer蒿讥。就不會(huì)引起崩潰了.
//計(jì)時(shí)器是否掛起
@property (nonatomic, assign) BOOL isSuspendTimer;
- (void)pauseCountDownTimer{//掛起
if(self.countDownTimer){
self.isSuspendTimer = YES;
dispatch_suspend(_countDownTimer);
}
}
- (void)resumeCountDownTimer{//恢復(fù)
if(self.countDownTimer){
self.isSuspendTimer = NO;
dispatch_resume(_countDownTimer);
}
}
- (void)clearLuckyBagInfo{
self.LuckyBagDetailModel = nil;
self.contentView = nil;
self.residueTime = 0;
//釋放定時(shí)器是先判斷該定時(shí)器是否被掛起
if (self.countDownTimer) {
if (self.isSuspendTimer) {
dispatch_resume(self.countDownTimer);
}
dispatch_cancel(self.countDownTimer);
self.countDownTimer = nil;
}
self.isSuspendTimer = NO;
self.liveDto = nil;
[self.winPriceArray removeAllObjects];
self.winPriceArray = nil;
if (self.tenSecondTimer) {
dispatch_cancel(self.tenSecondTimer);
self.tenSecondTimer = nil;
}
self.IsReciveLotteryMessage = NO;
self.timeGone = 0;
}
四.總結(jié)(dispatch_source_t)
dispatch_suspend 狀態(tài)下直接釋放當(dāng)前控制器或者釋放定時(shí)器璧尸,會(huì)導(dǎo)致定時(shí)器崩潰膝宁。并且初始狀態(tài)(未調(diào)用dispatch_resume)厉斟、掛起狀態(tài)珍语,都不能直接調(diào)用dispatch_source_cancel(timer),調(diào)用就會(huì)導(dǎo)致app閃退集漾。