Last active
February 1, 2024 08:03
-
-
Save plummer/99a21d013de0cfab773028f8507f92d5 to your computer and use it in GitHub Desktop.
DispatchGroup convenience wrapper for executing a series of blocks concurrently
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
extension DispatchGroup { | |
/// Completes a series of blocks and finally executes a completion handler. | |
/// | |
/// - Parameters: | |
/// - label: An optional DispatchQueue label (defaults to bg) | |
/// - qos: An optional DipatchQos (defaults to background) | |
/// - blocks: The series of blocks to call. | |
/// - completion: The final completion handler when all blocks are | |
/// finished. | |
/// | |
/// Ensure you call the completion handler in every block. | |
func complete( | |
label: String = "bg", | |
qos: DispatchQoS = .background, | |
_ blocks: ((() -> Void) -> Void)..., | |
completion: @escaping () -> Void) { | |
for block in blocks { | |
self.enter() | |
DispatchQueue( | |
label: label, | |
qos: qos).async { | |
block({ | |
self.leave() | |
}) | |
} | |
} | |
self.notify(queue: .main) { | |
completion() | |
} | |
} | |
} | |
// MARK: - Example | |
DispatchGroup().complete( | |
{ completion in sleep(1); print("execution 1"); completion() }, | |
{ completion in print("execution 2"); completion() }, | |
{ completion in print("execution 3"); completion() } | |
) { | |
print("end of group completion handler") | |
} |
@plummer I still don't understand how structured concurrency works. I have recently read that Task { Task { ... } }
is not equal to DispatchQueue.global().async { DispatchQueue.global().async { ... } }
and it confuses me.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Gargo, I recommend using structured concurrency if you are trying to do this in the future. This code is quite old and I wouldn't use it now.