3 May 2016 0min read

Quick and dirty debug timer

For those occasions when you need to check something some state at regular intervals.

Just drop this snippet at the top of your file:

static dispatch_source_t timer;
void DebugTimer(dispatch_block_t block)
{
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    double interval = 1;
    timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(timer, dispatch_time(DISPATCH_TIME_NOW, (int64_t)interval * NSEC_PER_SEC), (int64_t)interval * NSEC_PER_SEC, (1ull * NSEC_PER_SEC) / 10);
    dispatch_source_set_event_handler(timer, block);
    dispatch_resume(timer);
}

And then call this procedure, for example, in viewDidLoad:

DebugTimer(^{
    NSLog(@"What is wrong with you? %@", vipView);
});

Remember, this is just for debugging purposes.

If you enjoyed this post

Continue reading