Skip to content

Instantly share code, notes, and snippets.

@arroz
Created March 30, 2021 15:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arroz/1d2e8bc9b3103a5e903a7786c6a11534 to your computer and use it in GitHub Desktop.
Save arroz/1d2e8bc9b3103a5e903a7786c6a11534 to your computer and use it in GitHub Desktop.
Concurrent queue issue
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