Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dispatch_after recursion vs NSTimer scheduledtimerwithtimeinterval

I read a code, it checks data and update UI every second. It sounds like what we usually do using NSTimer scheduledtimerwithtimeinterval. But this code is implemented with recursively calling dispatch_after like this:

- (void) retriggerMethod {
    ... do stuff here, assuming you want to do it on first invocation ...
    dispatch_after( ..., ^{
        [self retriggerMethod];
    });
}

What's the difference between dispatch_after recursion and NSTimer scheduledtimerwithtimeinterval ? Is there potential risk when using the former? Cos I thought when you use it, the call stack would grow as long as not end this recursion.

like image 498
Gon Avatar asked Apr 17 '26 15:04

Gon


1 Answers

NSTimer:

1. Need a NSRunloop.
2. Can repeat.
3. Can be invalid anytime if u want to cancel.
4. Can only run with delegate.
5. High level API.

dispatch_after:

1. Can be run everywhere that u want with dispatch_queue.
2. Can't repeat by itself.
3. Can't be cancel.
4. It run as block.
5. GCD.
like image 66
Justlike Avatar answered Apr 19 '26 06:04

Justlike



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!