Created
March 30, 2021 15:24
-
-
Save arroz/1d2e8bc9b3103a5e903a7786c6a11534 to your computer and use it in GitHub Desktop.
Concurrent queue issue
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
let queue = DispatchQueue(label: "Queue", qos: .default, attributes: [.concurrent], autoreleaseFrequency: .workItem, target: .global(qos: .default)) | |
let cancellable = [1, 2, 3, 4, 5].publisher | |
.receive(on: queue) | |
.map { longRunningFunc(value: $0) } | |
.receive(on: DispatchQueue.main) | |
.sink (receiveCompletion: { completion in | |
Swift.print(completion) | |
}, receiveValue: { value in | |
Swift.print(value) | |
}) | |
func longRunningFunc(value: Int) -> Int { | |
let total = Int.random(in: 0..<100000) | |
// This is slow enough in a Playground | |
for i in 1..<total { | |
i + 1 | |
} | |
return value | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment