Skip to content

Commit

Permalink
修复GCD定时器引起的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
青锋 committed Sep 17, 2018
1 parent 6e5e76d commit 0dcac42
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 90 deletions.
2 changes: 1 addition & 1 deletion CLPlayer.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'CLPlayer'
s.version = '1.2.6'
s.version = '1.2.7'
s.summary = 'AVPlayer定制的视频播放器'
s.homepage = 'https://github.com/JmoVxia/CLPlayer'
s.license = 'MIT'
Expand Down
6 changes: 3 additions & 3 deletions CLPlayer/CLGCDTimerManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
delaySecs:(float)delaySecs
queue:(dispatch_queue_t)queue
repeats:(BOOL)repeats
action:(dispatch_block_t)action;
action:(void(^)(NSInteger actionTimes))action;
/*响应次数*/
@property (nonatomic, assign, readonly) NSInteger actionTimes;

Expand All @@ -29,7 +29,7 @@
/**恢复定时器*/
- (void)resumeTimer;
/**替换旧的响应*/
- (void)replaceOldAction:(dispatch_block_t)action;
- (void)replaceOldAction:(void(^)(NSInteger actionTimes))action;



Expand Down Expand Up @@ -57,7 +57,7 @@
delaySecs:(float)delaySecs
queue:(dispatch_queue_t)queue
repeats:(BOOL)repeats
action:(dispatch_block_t)action;
action:(void(^)(NSInteger actionTimes))action;

/**开始定时器*/
- (void)startTimer:(NSString *)timerName;
Expand Down
130 changes: 52 additions & 78 deletions CLPlayer/CLGCDTimerManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

@interface CLGCDTimer ()
/**响应*/
@property (nonatomic, copy) dispatch_block_t action;
@property (nonatomic, copy) void(^actionBlock)(NSInteger actionTimes);
/**线程*/
@property (nonatomic, strong) dispatch_queue_t serialQueue;
/**定时器名字*/
Expand All @@ -37,12 +37,12 @@ - (instancetype)initDispatchTimerWithName:(NSString *)timerName
delaySecs:(float)delaySecs
queue:(dispatch_queue_t)queue
repeats:(BOOL)repeats
action:(dispatch_block_t)action{
action:(void(^)(NSInteger actionTimes))action{
if (self = [super init]) {
self.timeInterval = interval;
self.delaySecs = delaySecs;
self.repeat = repeats;
self.action = action;
self.actionBlock = action;
self.timerName = timerName;
self.isRuning = NO;
self.serialQueue = dispatch_queue_create([[NSString stringWithFormat:@"CLGCDTimer.%p", self] cStringUsingEncoding:NSASCIIStringEncoding], DISPATCH_QUEUE_SERIAL);
Expand All @@ -56,12 +56,12 @@ - (instancetype)initDispatchTimerWithTimeInterval:(double)interval
delaySecs:(float)delaySecs
queue:(dispatch_queue_t)queue
repeats:(BOOL)repeats
action:(dispatch_block_t)action{
action:(void(^)(NSInteger actionTimes))action{
if (self = [super init]) {
self.timeInterval = interval;
self.delaySecs = delaySecs;
self.repeat = repeats;
self.action = action;
self.actionBlock = action;
self.timerName = nil;
self.isRuning = NO;
self.serialQueue = dispatch_queue_create([[NSString stringWithFormat:@"CLGCDTimer.%p", self] cStringUsingEncoding:NSASCIIStringEncoding], DISPATCH_QUEUE_SERIAL);
Expand All @@ -70,65 +70,52 @@ - (instancetype)initDispatchTimerWithTimeInterval:(double)interval
}
return self;
}
- (void)replaceOldAction:(dispatch_block_t)action {
self.action = action;
- (void)replaceOldAction:(void(^)(NSInteger actionTimes))action {
self.actionBlock = action;
}
/**开始定时器*/
- (void)startTimer {
//拿到当前线程线程
dispatch_async(self.serialQueue, ^{
dispatch_source_set_timer(self.timer_t, dispatch_time(DISPATCH_TIME_NOW, (NSInteger)(self.delaySecs * NSEC_PER_SEC)),(NSInteger)(self.timeInterval * NSEC_PER_SEC), 0 * NSEC_PER_SEC);
__weak typeof(self) weakSelf = self;
dispatch_source_set_event_handler(self.timer_t, ^{
if (self.action) {
self.action();
self.actionTimes ++;
}
if (!self.repeat) {
[weakSelf cancelTimer];
}
});
[self resumeTimer];
dispatch_source_set_timer(self.timer_t, dispatch_time(DISPATCH_TIME_NOW, (NSInteger)(self.delaySecs * NSEC_PER_SEC)),(NSInteger)(self.timeInterval * NSEC_PER_SEC), 0 * NSEC_PER_SEC);
dispatch_source_set_event_handler(self.timer_t, ^{
if (self.actionBlock) {
self.actionBlock(self.actionTimes);
self.actionTimes ++;
}
if (!self.repeat) {
[self cancelTimer];
}
});
[self resumeTimer];
}
/**执行一次定时器响应*/
- (void)responseOnceTimer {
self.isRuning = YES;
if (self.action) {
self.action();
if (self.actionBlock) {
self.actionBlock(self.actionTimes);
self.actionTimes ++;
}
self.isRuning = NO;
}
/**取消定时器*/
- (void)cancelTimer {
//拿到当前线程线程
dispatch_async(self.serialQueue, ^{
if (!self.isRuning) {
[self resumeTimer];
}
dispatch_source_cancel(self.timer_t);
});
if (!self.isRuning) {
[self resumeTimer];
}
dispatch_source_cancel(self.timer_t);
}
/**暂停定时器*/
- (void)suspendTimer {
//拿到当前线程线程
dispatch_async(self.serialQueue, ^{
if (self.isRuning) {
dispatch_suspend(self.timer_t);
self.isRuning = NO;
}
});
if (self.isRuning) {
dispatch_suspend(self.timer_t);
self.isRuning = NO;
}
}
/**恢复定时器*/
- (void)resumeTimer {
//拿到当前线程线程
dispatch_async(self.serialQueue, ^{
if (!self.isRuning) {
dispatch_resume(self.timer_t);
self.isRuning = YES;
}
});
if (!self.isRuning) {
dispatch_resume(self.timer_t);
self.isRuning = YES;
}
}
@end

Expand Down Expand Up @@ -184,7 +171,7 @@ - (void)scheduledDispatchTimerWithName:(NSString *)timerName
delaySecs:(float)delaySecs
queue:(dispatch_queue_t)queue
repeats:(BOOL)repeats
action:(dispatch_block_t)action {
action:(void(^)(NSInteger actionTimes))action {
NSParameterAssert(timerName);
__strong NSString *string = timerName;
CLGCDTimer *GCDTimer = [self timer:string];
Expand All @@ -207,32 +194,28 @@ - (void)startTimer:(NSString *)timerName {
__strong NSString *string = timerName;
CLGCDTimer *GCDTimer = [self timer:string];
if (!GCDTimer.isRuning && GCDTimer) {
//拿到当前线程线程
dispatch_async(GCDTimer.serialQueue, ^{
NSParameterAssert(string);
dispatch_source_t timer_t = GCDTimer.timer_t;
dispatch_source_set_timer(timer_t, dispatch_time(DISPATCH_TIME_NOW, (NSInteger)(GCDTimer.delaySecs * NSEC_PER_SEC)),(NSInteger)(GCDTimer.timeInterval * NSEC_PER_SEC), 0 * NSEC_PER_SEC);
__weak typeof(self) weakSelf = self;
dispatch_source_set_event_handler(timer_t, ^{
if (GCDTimer.action) {
GCDTimer.action();
GCDTimer.actionTimes ++;
}
if (!GCDTimer.repeat) {
[weakSelf cancelTimerWithName:string];
}
});
[self resumeTimer:string];
NSParameterAssert(string);
dispatch_source_t timer_t = GCDTimer.timer_t;
dispatch_source_set_timer(timer_t, dispatch_time(DISPATCH_TIME_NOW, (NSInteger)(GCDTimer.delaySecs * NSEC_PER_SEC)),(NSInteger)(GCDTimer.timeInterval * NSEC_PER_SEC), 0 * NSEC_PER_SEC);
dispatch_source_set_event_handler(timer_t, ^{
if (GCDTimer.actionBlock) {
GCDTimer.actionBlock(GCDTimer.actionTimes);
GCDTimer.actionTimes ++;
}
if (!GCDTimer.repeat) {
[self cancelTimerWithName:string];
}
});
[self resumeTimer:string];
}
}
#pragma mark - 执行一次定时器响应
- (void)responseOnceTimer:(NSString *)timerName {
__strong NSString *string = timerName;
CLGCDTimer *GCDTimer = [self timer:string];
GCDTimer.isRuning = YES;
if (GCDTimer.action) {
GCDTimer.action();
if (GCDTimer.actionBlock) {
GCDTimer.actionBlock(GCDTimer.actionTimes);
GCDTimer.actionTimes ++;
}
GCDTimer.isRuning = NO;
Expand All @@ -245,35 +228,26 @@ - (void)cancelTimerWithName:(NSString *)timerName {
[self resumeTimer:string];
}
if (GCDTimer) {
//拿到当前线程线程
dispatch_async(GCDTimer.serialQueue, ^{
dispatch_source_cancel(GCDTimer.timer_t);
[self.timerObjectCache removeObjectForKey:string];
});
dispatch_source_cancel(GCDTimer.timer_t);
[self.timerObjectCache removeObjectForKey:string];
}
}
#pragma mark - 暂停定时器
- (void)suspendTimer:(NSString *)timerName {
__strong NSString *string = timerName;
CLGCDTimer *GCDTimer = [self timer:string];
if (GCDTimer.isRuning && GCDTimer) {
//拿到当前线程线程
dispatch_async(GCDTimer.serialQueue, ^{
dispatch_suspend(GCDTimer.timer_t);
GCDTimer.isRuning = NO;
});
dispatch_suspend(GCDTimer.timer_t);
GCDTimer.isRuning = NO;
}
}
#pragma mark - 恢复定时器
- (void)resumeTimer:(NSString *)timerName {
__strong NSString *string = timerName;
CLGCDTimer *GCDTimer = [self timer:string];
if (!GCDTimer.isRuning && GCDTimer) {
//拿到当前线程线程
dispatch_async(GCDTimer.serialQueue, ^{
dispatch_resume(GCDTimer.timer_t);
GCDTimer.isRuning = YES;
});
dispatch_resume(GCDTimer.timer_t);
GCDTimer.isRuning = YES;
}
}
//MARK:JmoVxia---获取定时器
Expand Down
4 changes: 2 additions & 2 deletions CLPlayer/CLPlayerView.m
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,7 @@ - (CLGCDTimer *) sliderTimer{
_sliderTimer = [[CLGCDTimer alloc] initDispatchTimerWithTimeInterval:1.0f
delaySecs:0 queue:dispatch_get_main_queue()
repeats:YES
action:^{
action:^(NSInteger actionTimes) {
__typeof(&*weakSelf) strongSelf = weakSelf;
[strongSelf timeStack];
}];
Expand All @@ -920,7 +920,7 @@ - (CLGCDTimer *) tapTimer{
delaySecs:self.config.toolBarDisappearTime
queue:dispatch_get_main_queue()
repeats:YES
action:^{
action:^(NSInteger actionTimes) {
__typeof(&*weakSelf) strongSelf = weakSelf;
[strongSelf disappear];
}];
Expand Down
12 changes: 6 additions & 6 deletions CLPlayerDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
0C9020A7210EE96A00687A7E /* CLGCDTimerManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 0C9020A5210EE96A00687A7E /* CLGCDTimerManager.m */; };
0CD6AE3D214FACB000BCE29D /* CLGCDTimerManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 0CD6AE3B214FACAF00BCE29D /* CLGCDTimerManager.m */; };
453E2AAA1F3C97900003AB0C /* AILoadingView.m in Sources */ = {isa = PBXBuildFile; fileRef = 453E2AA91F3C97900003AB0C /* AILoadingView.m */; };
52CFF55482649C3F8FF48753 /* libPods-CLPlayerDemo.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 188363D6831D09896A05B358 /* libPods-CLPlayerDemo.a */; };
5B1FC4C81DC850820036C489 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 5B1FC4C71DC850820036C489 /* main.m */; };
Expand Down Expand Up @@ -38,8 +38,8 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
0C9020A5210EE96A00687A7E /* CLGCDTimerManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CLGCDTimerManager.m; sourceTree = "<group>"; };
0C9020A6210EE96A00687A7E /* CLGCDTimerManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CLGCDTimerManager.h; sourceTree = "<group>"; };
0CD6AE3B214FACAF00BCE29D /* CLGCDTimerManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CLGCDTimerManager.m; sourceTree = "<group>"; };
0CD6AE3C214FACAF00BCE29D /* CLGCDTimerManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CLGCDTimerManager.h; sourceTree = "<group>"; };
133621D737F2050001C8A017 /* Pods-CLPlayerDemo.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-CLPlayerDemo.debug.xcconfig"; path = "Pods/Target Support Files/Pods-CLPlayerDemo/Pods-CLPlayerDemo.debug.xcconfig"; sourceTree = "<group>"; };
188363D6831D09896A05B358 /* libPods-CLPlayerDemo.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-CLPlayerDemo.a"; sourceTree = BUILT_PRODUCTS_DIR; };
453E2AA81F3C97900003AB0C /* AILoadingView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AILoadingView.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -304,8 +304,8 @@
5BDEA70C1E63F4BB00953D5D /* CLPlayer */ = {
isa = PBXGroup;
children = (
0C9020A6210EE96A00687A7E /* CLGCDTimerManager.h */,
0C9020A5210EE96A00687A7E /* CLGCDTimerManager.m */,
0CD6AE3C214FACAF00BCE29D /* CLGCDTimerManager.h */,
0CD6AE3B214FACAF00BCE29D /* CLGCDTimerManager.m */,
5BB55D091FC549A800CC8327 /* CLPlayerView.h */,
5BB55D081FC549A700CC8327 /* CLPlayerView.m */,
5BDEA70F1E63F4BB00953D5D /* CLPlayerMaskView.h */,
Expand Down Expand Up @@ -467,7 +467,7 @@
5BB4D0EE1F31B7A2007A1742 /* CLViewController5.m in Sources */,
5BDEA7161E63F4BB00953D5D /* CLPlayerMaskView.m in Sources */,
5BDEA7181E63F4BB00953D5D /* CLSlider.m in Sources */,
0C9020A7210EE96A00687A7E /* CLGCDTimerManager.m in Sources */,
0CD6AE3D214FACB000BCE29D /* CLGCDTimerManager.m in Sources */,
5BB4D0EB1F31B775007A1742 /* CLViewController3.m in Sources */,
5BB4D0D91F31A1F1007A1742 /* CLTableViewViewController.m in Sources */,
5BB4D0F41F331FA1007A1742 /* CLNavigationViewController.m in Sources */,
Expand Down
Binary file not shown.

0 comments on commit 0dcac42

Please sign in to comment.