//
|
// HDLSmartLinkTimerManager.m
|
// HdlSmartIoT
|
//
|
// Created by 陈启扬 on 2021/9/30.
|
//
|
|
#import "HDLLinphoneTimerManager.h"
|
@interface HDLLinphoneTimerManager()
|
@property (nonatomic, strong) NSMutableDictionary *sendTimers;//发送数据时的计时器
|
|
//@property (nonatomic, strong) NSMutableDictionary *timerOuts;//倒计时的timeOut(用于重发,key为timerName,value为对应timeOut)
|
//
|
//@property (nonatomic, assign) NSTimeInterval timeStamp;//时间戳(用于记录退出应用时的时间)
|
//
|
//@property (nonatomic, strong) NSNumber *timeOutNum;//
|
|
|
@end
|
@implementation HDLLinphoneTimerManager
|
|
+ (instancetype)sharedInstance {
|
static HDLLinphoneTimerManager *instance = nil;
|
static dispatch_once_t onceToken;
|
dispatch_once(&onceToken, ^{
|
instance = [[HDLLinphoneTimerManager alloc] init];
|
});
|
return instance;
|
}
|
|
-(instancetype)init{
|
self = [super init];
|
if (self) {
|
self.sendTimers=[[NSMutableDictionary alloc] init];
|
|
// [self observeApplicationActionNotification];
|
}
|
return self;
|
}
|
|
///*注册程序生命周期通知
|
// */
|
//- (void)observeApplicationActionNotification {
|
// [[NSNotificationCenter defaultCenter] removeObserver:self];
|
// [[NSNotificationCenter defaultCenter] addObserver:self selector: @selector(applicationDidBecomeActive) name:UIApplicationDidBecomeActiveNotification object:nil];
|
// [[NSNotificationCenter defaultCenter] addObserver:self selector: @selector(applicationDidEnterBackground) name: UIApplicationDidEnterBackgroundNotification object:nil];
|
//}
|
//- (void)applicationDidEnterBackground {
|
// _timeStamp = [NSDate date].timeIntervalSince1970;
|
//}
|
//- (void)applicationDidBecomeActive {
|
// NSTimeInterval timeInterval = [NSDate date].timeIntervalSince1970-_timeStamp; //进行时间差计算操作
|
// _timeStamp = 0;
|
// for (int i=0; i<self.timerOuts.allKeys.count; i++) {
|
// _timeOutNum=self.timerOuts.allValues[i];
|
// NSTimeInterval ret = [_timeOutNum doubleValue] - timeInterval;
|
// if (ret > 0) {
|
// _timeOutNum = [NSNumber numberWithInteger:ret];
|
// } else {
|
// _timeOutNum = [NSNumber numberWithInteger:0];
|
// }
|
// }
|
//
|
//}
|
|
//倒计时
|
- (void)scheduleTimerWithInterval:(double)interval timerName:(NSString *)timerName count:(HDLLPCoutdownBlock)count
|
finished:(dispatch_block_t)finished
|
{
|
// [self.sendTimerNames addObject:timerName];
|
__block double timeout =interval;
|
|
|
|
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
|
dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
|
[self.sendTimers setObject:timer forKey:timerName];
|
// HDLNSLog(@"self.sendTimers:%@",self.sendTimers);
|
dispatch_source_set_timer(timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0);//
|
|
dispatch_source_set_event_handler(timer, ^{
|
@autoreleasepool {
|
if (timeout<=0) {
|
dispatch_source_cancel(timer);
|
dispatch_async(dispatch_get_main_queue(), ^{
|
finished();
|
});
|
}else{
|
dispatch_async(dispatch_get_main_queue(), ^{
|
count(timeout);
|
});
|
}
|
timeout--;
|
}
|
});
|
|
dispatch_resume(timer);
|
}
|
|
///**
|
// 启动一个timer
|
// @param interval 执行的时间间隔(单位是秒)
|
// @param timerName timer的名称,作为唯一标识。
|
// @param count 倒计时block
|
// @param finished 时间间隔到点时执行的block。
|
// */
|
//- (void)scheduleTimerWithInterval:(double)interval timerName:(NSString *)timerName count:(HDLOPPCoutdownBlock)count
|
// finished:(dispatch_block_t)finished
|
//{
|
//// [self.sendTimerNames addObject:timerName];
|
// __block NSNumber *timeout =[NSNumber numberWithDouble:interval];
|
//
|
// [self.timerOuts setObject:timeout forKey:timerName];
|
//
|
// dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
|
// dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
|
// [self.sendTimers setObject:timer forKey:timerName];
|
//// HDLNSLog(@"self.sendTimers:%@",self.sendTimers);
|
// dispatch_source_set_timer(timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0);//
|
//
|
// dispatch_source_set_event_handler(timer, ^{
|
// @autoreleasepool {
|
// double timeoutD=[timeout doubleValue];
|
// if (timeoutD<=0) {
|
// dispatch_source_cancel(timer);
|
// dispatch_async(dispatch_get_main_queue(), ^{
|
// finished();
|
// });
|
// }else{
|
// dispatch_async(dispatch_get_main_queue(), ^{
|
// count(timeoutD);
|
// });
|
// }
|
// timeoutD--;
|
// timeout=[NSNumber numberWithDouble:timeoutD];
|
// [self.timerOuts setObject:timeout forKey:timerName];
|
// }
|
// });
|
//
|
// dispatch_resume(timer);
|
//}
|
|
/*可设置单位的倒计时
|
@param interval 总时间
|
@param unit 计时单位
|
*/
|
- (void)scheduleTimerWithInterval:(double)interval unit:(double)unit timerName:(NSString *)timerName count:(HDLLPCoutdownBlock)count
|
finished:(dispatch_block_t)finished
|
{
|
[self cancelTimerByName:timerName];
|
__block double timeout =interval;
|
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
|
dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
|
[self.sendTimers setObject:timer forKey:timerName];
|
dispatch_source_set_timer(timer,dispatch_walltime(NULL, 0),unit*NSEC_PER_SEC, 0);//
|
|
dispatch_source_set_event_handler(timer, ^{
|
@autoreleasepool {
|
if (timeout<=0) {
|
dispatch_source_cancel(timer);
|
dispatch_async(dispatch_get_main_queue(), ^{
|
finished();
|
});
|
}else{
|
dispatch_async(dispatch_get_main_queue(), ^{
|
count(timeout);
|
});
|
}
|
timeout-=unit;
|
}
|
});
|
|
dispatch_resume(timer);
|
}
|
|
/**
|
启动一个计时
|
@param rateTime 执行的时间间隔(单位是秒)
|
@param timerName timer的名称,作为唯一标识。
|
@param count 计时block
|
*/
|
- (void)scheduleTimerWithRateTime:(float)rateTime timerName:(NSString *)timerName count:(HDLLPCoutdownVoidBlock)count{
|
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
|
dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
|
[self.sendTimers setObject:timer forKey:timerName];
|
|
dispatch_source_set_timer(timer,dispatch_walltime(NULL, 0),rateTime*NSEC_PER_SEC, 0);//
|
|
dispatch_source_set_event_handler(timer, ^{
|
@autoreleasepool {
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
count();
|
});
|
}
|
});
|
|
dispatch_resume(timer);
|
|
}
|
|
- (void)cancelTimerByName:(NSString *)timerName{
|
if ([self.sendTimers objectForKey:timerName]) {
|
dispatch_source_t timer = [self.sendTimers objectForKey:timerName];
|
dispatch_source_cancel(timer);
|
[self.sendTimers removeObjectForKey:timerName];
|
|
}
|
// dispatch_source_t timer = [self.sendTimers objectForKey:timerName];
|
|
// if (timer){
|
// dispatch_source_cancel(timer);
|
// [self.sendTimers removeObjectForKey:timerName];
|
// }
|
// [self.sendTimerNames removeObject:timerName];
|
|
|
|
}
|
|
- (void)resumeTimerByName:(NSString *)timerName{
|
dispatch_source_t timer = [self.sendTimers objectForKey:timerName];
|
|
if (!timer)
|
return;
|
|
dispatch_resume(timer);
|
|
}
|
|
- (void)suspendTimerByName:(NSString *)timerName{
|
dispatch_source_t timer = [self.sendTimers objectForKey:timerName];
|
|
if (!timer)
|
return;
|
|
dispatch_suspend(timer);
|
}
|
|
/*获取timer
|
@param timerName timer的名称。
|
*/
|
-(dispatch_source_t)timerByNmae:(NSString *)timerName{
|
if ([self.sendTimers objectForKey:timerName]) {
|
dispatch_source_t timer = [self.sendTimers objectForKey:timerName];
|
return timer;
|
}else{
|
return nil;
|
}
|
}
|
@end
|