how to wait for coroutine in non-suspend function?...
# announcements
i
how to wait for coroutine in non-suspend function?
Copy code
fun f () {
  val result = async { ktor-client-function... return@async 1 }
  return result
}
a
runBlocking
, not that I recommend it
your code doesn’t make sense, you want to run async but wait it from non-suspend function.
😆 1
K 1
async is useless in your example
i
because I use Ktor, and its function need to call in coroutine, so I use async to contain it
a
ok, runBlocking has the scope. I guess you were just using async to illustrate what you want
a
Why not “moving the asynchronicity upwards”? i.e. make your
f
function suspended and do the
runBlocking
at the beginning of your program
i
the original question is I use Glide in FileAdapter for a ListView, and in that Glide.load(url), the url is a redirect url, I need use Glide.load(httpclient.get(url)), but httpclient need to call in a suspend function, and FileAdapter.getView isn't a suspend function, how I can call ktor's httpclient in it?
a
load it from a scope
viewModelScope, lifecycleScope, fragmentLifecycleScope, viewLifecycleScope, whatever
makes sure you don’t leak it
i
load what from a scop?
scope.launch { Glide.load... } in getView?
a
yep
You’ll get NetworkOnMainThreadException anyway if you run okhttp from the main thread
Glide should be handling redirect URLs properly as well if client is configured