AwaitKit
AwaitKit copied to clipboard
Best practice in multi-thread app
My app have a dispatchQ
(DispatchQueue) for network requests and most other operations, and some other queues for heavy computing. I use PromiseKit like this,
fetchData().then { objects in
let tasks = objects.map { calculate($0 }
return when(fulfilled: tasks)
}.done {
}.catch { error in
}
PromiseKit is simple, I just use promise to replace those callbacks. Now I try to use AwaitKit, but soon I find out that await
blocks current queue. dispatchQ
can never be blocked because while waiting for network responses, the app might doing other small tasks in dispatchQ
. I try to create awaitQ
for waiting tasks like this,
let awaitQ = DispatchQueue(label: "await")
awaitQ.ak.async {
let objects = try await(fetchData())
let tasks = let tasks = objects.map { calculate($0 }
try await(when(fulfilled: tasks))
}
However, because dispatchQ
have other tasks, awaitQ
will still be blocked while waiting for one task. I will try to use DispatchQueue.global(qos: .userInitiated)
to start more threads for waiting.
So, what is the best practice for app like this? Is it OK to start lots of threads for await
?
Thanks in advance!