blob: ba2e898a6968dcc45aac32a6a0d4e811399c521a (
plain)
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
|
//
// dispatch_timer.c
// MobileNav
//
// Created by Muller, Alexander (A.) on 5/12/16.
// Copyright © 2016 Alex Muller. All rights reserved.
//
#include "dispatch_timer.h"
dispatch_source_t dispatch_create_timer(double afterInterval, bool repeating, dispatch_block_t block) {
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);
dispatch_source_set_timer(timer,
dispatch_time(DISPATCH_TIME_NOW, afterInterval * NSEC_PER_SEC),
afterInterval * NSEC_PER_SEC,
(1ull * NSEC_PER_SEC) / 10);
dispatch_source_set_event_handler(timer, ^{
if (!repeating) {
dispatch_stop_timer(timer);
}
if (block) {
block();
}
});
dispatch_resume(timer);
return timer;
}
void dispatch_stop_timer(dispatch_source_t timer) {
dispatch_source_set_event_handler(timer, NULL);
dispatch_source_cancel(timer);
}
|