ios - serialQueue 中的 performSelectorWithDelay

标签 ios objective-c

我有一个串行队列,我使用该队列调用 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/

相关文章:

ios - 应用内购买恢复未完成

ios - 如果另一列存在,SQLite 更新列

iphone - pageviewcontroller 中的 PageCurlEffect 与工具栏重叠

ios - 检查 Objective-C 字符串中的特定字符

iphone - UIScrollview 中的操作表

ios - TableView 不从数据库重新加载数据

ios - 在同一个 Storyboard 中重用另一个表 VC 中的单元格

ios - 使用数组数据更新 TableView

objective-c - 一些协议(protocol)问题

iphone - iPhone/iPad 应用程序启动时发出警报