如何快速获得异步延迟?
我有3个函数--假设函数第一()、第二()和第三()--它们是异步调用的。但是,在第二个()函数中放置10秒的延迟后,第三个函数将在10秒之后调用,而不是我只希望函数中的代码在10秒之后调用,而不是第三个函数。
提前谢谢。
发布于 2016-09-26 12:42:57
假设..。
..。您可以使用OperationQueue (以前是Swift 2中的NSOperationQueue ):
func first() { print("First") }
func second() { print("Second") }
func third() { print("Third") }
// Since we will block the queue while wait for all three functions to complete,
// dispatch it to a background queue. Don't block the main queue
DispatchQueue.global(qos: .background).async {
let queue = OperationQueue()
queue.addOperation(first)
queue.addOperation(second)
queue.addOperation(third)
queue.waitUntilAllOperationsAreFinished()
// Now all your functions are complete
}请注意,即使按顺序添加了函数,也无法确定它们的执行顺序。
https://stackoverflow.com/questions/39701790
复制相似问题