forked from YouXianMing/GCD-Program
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GCDTimer.m
59 lines (49 loc) · 1.33 KB
/
GCDTimer.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//
// GCDTimer.m
// GCD
//
// http://home.cnblogs.com/u/YouXianMing/
// https://github.com/YouXianMing
//
// Created by XianMingYou on 15/3/15.
// Copyright (c) 2015年 XianMingYou. All rights reserved.
//
#import "GCDTimer.h"
#import "GCDQueue.h"
@interface GCDTimer ()
@property (strong, readwrite, nonatomic) dispatch_source_t dispatchSource;
@end
@implementation GCDTimer
- (instancetype)init
{
self = [super init];
if (self) {
self.dispatchSource = \
dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0));
}
return self;
}
- (instancetype)initInQueue:(GCDQueue *)queue {
self = [super init];
if (self) {
self.dispatchSource = \
dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue.dispatchQueue);
}
return self;
}
- (void)event:(dispatch_block_t)block timeInterval:(uint64_t)interval {
dispatch_source_set_timer(self.dispatchSource,
dispatch_time(DISPATCH_TIME_NOW, 0),
interval,
0);
dispatch_source_set_event_handler(self.dispatchSource, ^{
block();
});
}
- (void)start {
dispatch_resume(self.dispatchSource);
}
- (void)destroy {
dispatch_source_cancel(self.dispatchSource);
}
@end