gpt4 book ai didi

ios - dispatch_after 递归 vs NSTimer scheduledtimerwithtimeinterval

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:55:35 26 4
gpt4 key购买 nike

我读了一段代码,它每秒检查数据并更新 UI。这听起来像我们通常使用 NSTimer scheduledtimerwithtimeinterval 所做的事情。但是这段代码是通过递归调用 dispatch_after 实现的,如下所示:

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

dispatch_after recursionNSTimer scheduledtimerwithtimeinterval 有什么区别?使用前者是否存在潜在风险?因为我认为当您使用它时,只要不结束此递归,调用堆栈就会增长。

最佳答案

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.

调度后:

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.

关于ios - dispatch_after 递归 vs NSTimer scheduledtimerwithtimeinterval,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34854747/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com