forked from cheesemaker/toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUUTimer.m
More file actions
227 lines (183 loc) · 5.84 KB
/
UUTimer.m
File metadata and controls
227 lines (183 loc) · 5.84 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
//
// UUTimer.m
// Useful Utilities - GCD based timer
//
// License:
// You are free to use this code for whatever purposes you desire. The only
// requirement is that you smile everytime you use it.
//
#import "UUTimer.h"
#import "UUDictionary.h"
#import "UUMacros.h"
#ifndef UUTimerLog
#ifdef DEBUG
#define UUTimerLog(fmt, ...) UUDebugLog(fmt, ##__VA_ARGS__)
#else
#define UUTimerLog(fmt, ...)
#endif
#endif
// Force UUTimerLog to be disabled all the time. Comment these two lines to
// enable debug logging again
#undef UUTimerLog
#define UUTimerLog(fmt, ...)
@interface UUTimer ()
@property (nonnull, nonatomic, copy, readwrite) NSString* timerId;
@property (nullable, nonatomic, strong, readwrite) id userInfo;
@property (assign, readwrite) NSTimeInterval interval;
@property (assign, readwrite) BOOL repeat;
@property (atomic, strong) dispatch_source_t dispatchSource;
@end
@implementation UUTimer
+ (nonnull dispatch_queue_t) backgroundTimerQueue
{
return dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
}
+ (nonnull dispatch_queue_t) mainThreadTimerQueue
{
return dispatch_get_main_queue();
}
+ (nonnull NSMutableDictionary<NSString*, UUTimer*>*) activeTimers
{
static NSMutableDictionary<NSString*, UUTimer*>* theActiveTimers = nil;
static dispatch_once_t onceToken;
dispatch_once (&onceToken, ^
{
theActiveTimers = [NSMutableDictionary dictionary];
});
return theActiveTimers;
}
+ (nullable instancetype) findActiveTimer:(nonnull NSString*)timerId
{
return [[self activeTimers] uuSafeGet:timerId forClass:[UUTimer class]];
}
+ (nonnull NSArray<UUTimer*>*) listActiveTimers
{
return [[self activeTimers] allValues];
}
+ (void) addTimer:(nonnull UUTimer*)timer
{
NSMutableDictionary* d = [self activeTimers];
@synchronized (d)
{
[d setValue:timer forKey:timer.timerId];
}
}
+ (void) removeTimer:(nonnull UUTimer*)timer
{
NSMutableDictionary* d = [self activeTimers];
@synchronized (d)
{
[d uuSafeRemove:timer.timerId];
}
}
- (nonnull id) initWithInterval:(NSTimeInterval)interval
userInfo:(nullable id)userInfo
repeat:(BOOL)repeat
queue:(nonnull dispatch_queue_t)queue
block:(nonnull UUTimerBlock)block
{
return [self initWithId:[[NSUUID UUID] UUIDString]
interval:interval
userInfo:userInfo
repeat:repeat
queue:queue
block:block];
}
- (nonnull id) initWithId:(nonnull NSString*)timerId
interval:(NSTimeInterval)interval
userInfo:(nullable id)userInfo
repeat:(BOOL)repeat
queue:(nonnull dispatch_queue_t)queue
block:(nonnull UUTimerBlock)block
{
self = [super init];
if (self)
{
self.timerId = timerId;
self.userInfo = userInfo;
self.interval = interval;
self.repeat = repeat;
dispatch_source_t src = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
if (src)
{
uint64_t nanoInterval = (interval * NSEC_PER_SEC);
if (!repeat)
{
nanoInterval = DISPATCH_TIME_FOREVER;
}
dispatch_source_set_timer(src, dispatch_time(DISPATCH_TIME_NOW, interval * NSEC_PER_SEC), nanoInterval, (1ull * NSEC_PER_SEC) / 10);
dispatch_source_set_event_handler(src, ^
{
UUTimerLog(@"Invoking timer block for timer %@, userInfo: %@", self.timerId, self.userInfo);
block(self);
if (!repeat)
{
[self cancel];
}
});
self.dispatchSource = src;
}
}
return self;
}
- (void) start
{
@synchronized (self)
{
UUTimerLog(@"Starting timer %@, interval: %@, repeat: %@, dispatchSource: %@, userInfo: %@",
self.timerId, @(self.interval), @(self.repeat), self.dispatchSource, self.userInfo);
if (self.dispatchSource)
{
[[self class] addTimer:self];
dispatch_resume(self.dispatchSource);
}
else
{
UUTimerLog(@"Cannot start timer %@ because dispatch source is nil", self.timerId);
}
}
}
- (void) cancel
{
@synchronized (self)
{
UUTimerLog(@"Cancelling timer %@, dispatchSource: %@, userInfo: %@", self.timerId, self.dispatchSource, self.userInfo);
if (self.dispatchSource)
{
dispatch_source_cancel(self.dispatchSource);
self.dispatchSource = nil;
}
[[self class] removeTimer:self];
}
}
@end
@implementation UUTimer (WatchdogTimers)
+ (void) startWatchdogTimer:(nonnull NSString*)timerId
timeout:(NSTimeInterval)timeout
userInfo:(nullable id)userInfo
block:(nonnull void (^)(id _Nullable userInfo))block
{
[self cancelWatchdogTimer:timerId];
if (timeout > 0)
{
UUTimer* t = [[UUTimer alloc] initWithId:timerId
interval:timeout
userInfo:userInfo
repeat:NO
queue:[self backgroundTimerQueue]
block:^(UUTimer * _Nonnull timer)
{
if (block)
{
block(timer.userInfo);
}
}];
[t start];
}
}
+ (void) cancelWatchdogTimer:(nonnull NSString*)timerId
{
UUTimer* t = [self findActiveTimer:timerId];
[t cancel];
}
@end