I've recently started learning how to use Swift concurrency. There is a scenario where I would like to determine if there is a better solution.
There's an async function here, and I want to be able to share the waiting state with different threads. It looks like I'm using RxSwift's share()
operator.
Currently I use Task
to share the waiting status as following
actor Foo {
private var task: Task<Int, Never>?
private var value: Int = 0
func getValue() async -> Int {
guard let task else { return value }
return await task.value
}
func refresh() async {
guard task == nil else { return }
let task = Task {
return await SomeNetworkCall()
}
self.task = task
_ = await task.value
self.task = nil
}
}
I‘d like to know if there is a better way to solve this situation.