Thank you Quinn for the explanation! Where I was trying to go with this is to use Swift Concurrency to get a similar in-order behavior for closure execution as an old pattern I'd use with Grand Central Dispatch using a semaphore. A sketch from memory:
final class GCDSingleOperationRunner {
let serialQueue = DispatchQueue(label: "singleOperationRunner.queue")
typealias OperationAction = (_ completion: @escaping () -> ()) -> ()
func run(operation: @escaping OperationAction) {
serialQueue.async {
let semaphore = DispatchSemaphore(value: 0)
operation {
semaphore.signal()
}
semaphore.wait()
}
}
}
which would take in asynchronous methods with a completion handler:
func syncWithRemote(completion: () -> Void) -> Void {
fetchDataFromNetwork { data in
writeToLocalDatabase(data) {
completion()
}
}
}
singleOperationRunner.run(operation: syncWithRemote)
singleOperationRunner.run(operation: syncWithRemote)
Topic:
Programming Languages
SubTopic:
Swift
Tags: