Hello, I have an object Ktor {
where I have its methods like
suspend fun <T>getAsync(url:String) = coroutineScope {
println(url)
return@coroutineScope httpClient.get<T>(url)
}
But I can’t do this even if I wrap it in a class:
class Get<T:Any>(){
suspend fun getAsync(url:String) = coroutineScope {
println(url)
return@coroutineScope httpClient.get<T>(url)
}
}
So, the way I should do it according to the hint is to make an inline function:
inline fun <reified T> get(url: String) = runBlocking {
println(url)
return@runBlocking httpClient.get<T>(url)
}
But in this way I need to use runBlocking
instead of coroutineScope.async
.
There are 2 questions:
- How to make it properly?
- How much runBlocking can decrease the performance (especially with multiple HTTP requests)?
The way I can do it is to specify :
suspend fun test(url: String): HttpResponse = coroutineScope {
return@coroutineScope httpClient.get<HttpResponse>(url)
}
But then I need to add the JSON parcelizer to all places this method is used.