@deimus I also ran into the problem of changing the sending interval. Please refer below code
dispatch_source_t aTimer; void someMethod (){ printf("In timer\n"); dispatch_source_set_timer(aTimer, dispatch_walltime(NULL, 0), 10ull * NSEC_PER_SEC, 1ull * NSEC_PER_SEC); } dispatch_source_t CreateDispatchTimer(uint64_t interval, uint64_t leeway, dispatch_queue_t queue) { dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue); if (timer) { dispatch_source_set_timer(timer, dispatch_walltime(NULL, 0), interval, leeway); dispatch_source_set_event_handler(timer, ^{ someMethod(); }); dispatch_resume(timer); } return timer; } void MyCreateTimer() { aTimer = CreateDispatchTimer(1ull * NSEC_PER_SEC, 1ull * NSEC_PER_SEC, dispatch_get_main_queue()); [aTimer retain];
According to your answer, the timer interval should change when dispatch_source_set_timer () is called with a new time interval, in someMethod (). But this does not work as expected! The timer fires in an explosion. It looks like the timer interval is set to zero. I am new to iOS programming. Please let me know if I am doing wrong.
source share