gpt4 book ai didi

ios - serialQueue 中的 performSelectorWithDelay

转载 作者:行者123 更新时间:2023-11-28 21:00:31 25 4
gpt4 key购买 nike

我有一个串行队列,我使用该队列调用 performSelectorWithDelay,如下所示

dispatch_async(serialQueue, ^(void) {
[ self 执行选择器:@选择器(fetchConfigFromNetwork)withObject:nil afterDelay:rootConfig.waitTime];
});

但是,方法 fetchConfigFromNetwork 永远不会被调用。但是,如果我使用 mainQueue 而不是 serialQueue - 它开始工作。

无法理解这里发生了什么以及如何解决它?

最佳答案

文档中解释了为什么您的代码不起作用:https://developer.apple.com/documentation/objectivec/nsobject/1416176-performselector?language=occ

This method registers with the runloop of its current context, and depends on that runloop being run on a regular basis to perform correctly. One common context where you might call this method and end up registering with a runloop that is not automatically run on a regular basis is when being invoked by a dispatch queue. If you need this type of functionality when running on a dispatch queue, you should use dispatch_after and related methods to get the behavior you want.

我假设您希望在串行队列上延迟调用该方法。最直接(也是推荐的方式)是使用 dispatch_after:

__weak typeof(self) wself = self;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(rootConfig.waitTime * NSEC_PER_SEC)), serialQueue, ^{
[wself fetchConfigFromNetwork];
});

关于ios - serialQueue 中的 performSelectorWithDelay,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49284978/

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