Is run blocking is the right way to go if I have t...
# android
r
Is run blocking is the right way to go if I have to wait for the response from an http request inorder to retry another failed http request? The failed http request callback is received on an overridden method of an interface (part of another library) and it is not a suspending function. If not, please suggest the right approach. Thanks in advance.
c
Callback-based APIs can be tied into a coroutine with
suspendCancellableCoroutine
, which allows you to “wait” for a result without blocking a thread.
runBlocking
shouldn’t really be used anywhere but a
main()
function https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/suspend-cancellable-coroutine.html
👍 1
r
@Casey Brooks Sorry If I wasn't clear with my question. The waiting part is absolutely fine, but the call for that is made from a sync fun which will return a value before the suspending function returns it's result (which is essential for the fun return value) https://stackoverflow.com/q/54375195/5803873 This so question is very close to my doubt and it's using
runBlocking
as a solution.