ios - 在 iOS swift 中异步/并发/并行运行任务

标签 ios swift swift4 grand-central-dispatch dispatch-queue

如何一次执行上述所有任务以提高速度。

self.passingMsgIdsTofetchMsgss(messageIDs : msgIDBatches[0]) 

self.passingMsgIdsTofetchMsgss(messageIDs : msgIDBatches[1]) 

self.passingMsgIdsTofetchMsgss(messageIDs : msgIDBatches[2]) 

self.passingMsgIdsTofetchMsgss(messageIDs : msgIDBatches[3])

最佳答案

您要查找的是 DispatchQueue 上的类函数 concurrentPerform

例如:

DispatchQueue.concurrentPerform(iterations: msgIDBatches.count) { (index) in
    self.passingMsgIdsTofetchMsgss(messageIDs : msgIDBatches[index])    
}

如果您正在更新 UI 并且确保 passingMsgIdsTofetchMsgss 是线程安全的,您显然需要小心回调主队列。也值得使用 time profiler 检查这就是性能瓶颈所在。

另一个选项是 OperationQueue,您可以将所有提取添加到队列中并同时执行它们。

关于ios - 在 iOS swift 中异步/并发/并行运行任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49775203/

相关文章:

ios - 在 swift 中拖放 collectionViewCell

ios - 模态视图中的 UIButton 通过 UIToolBar 通过 UITabBar 未检测到触摸

ios - Coinbase API 无效请求(oauth token )

ios - 使用未解析的标识符 'NavigationLink' ;你的意思是 'NavigationView' 吗? - swift 用户界面

ios - 没有更多上下文swift4,表达式类型不明确

ios - 在 UITableView 中分页数据

ios - 德尔福,FireMonkey : How to find unique hardware id in OSX and iOS

swift - 使用 swift 添加 NSNotification Observer

swift - 在 Swift 中迭代并将图像和标题从数组 (image_url) 加载到 ViewController 中的正确方法

ios - Swift KVO 持有对象引用计数