如何一次执行以上所有任务以提高速度。
self.passingMsgIdsTofetchMsgss(messageIDs : msgIDBatches[0])
self.passingMsgIdsTofetchMsgss(messageIDs : msgIDBatches[1])
self.passingMsgIdsTofetchMsgss(messageIDs : msgIDBatches[2])
self.passingMsgIdsTofetchMsgss(messageIDs : msgIDBatches[3])发布于 2018-04-11 20:41:14
您需要的是DispatchQueue concurrentPerform上的类函数
例如:
DispatchQueue.concurrentPerform(iterations: msgIDBatches.count) { (index) in
self.passingMsgIdsTofetchMsgss(messageIDs : msgIDBatches[index])
}如果要更新UI,显然需要小心在主队列上回调,同时还要确保passingMsgIdsTofetchMsgss是线程安全的。同样值得使用time profiler检查的是,这实际上是性能瓶颈所在。
另一个选项是OperationQueue,您可以将所有的fetches添加到一个队列中,并同时执行它们。
发布于 2018-10-19 01:21:08
Swift 4.1。首先创建一个并发队列
private let concurrentPhotoQueue = DispatchQueue(label: "App_Name", attributes: .concurrent)现在将您的工作分派到并发队列
concurrentPhotoQueue.async(flags: .barrier) { [weak self] in
// 1
guard let weakSelf = self else {
return
}
// 2 Perform your task here
weakSelf.passingMsgIdsTofetchMsgss(messageIDs : msgIDBatches[0])
weakSelf.passingMsgIdsTofetchMsgss(messageIDs : msgIDBatches[1])
weakSelf.passingMsgIdsTofetchMsgss(messageIDs : msgIDBatches[2])
weakSelf.passingMsgIdsTofetchMsgss(messageIDs : msgIDBatches[3])
// 3
DispatchQueue.main.async { [weak self] in
// Update your UI here
}
}https://stackoverflow.com/questions/49775203
复制相似问题