I'm just starting learning coroutines and managed to write a simple blocking one, that's aimed on a retry handling of a remote rest service. It returns me an eception if he can't currently handle foo update. But we have an agreement that i'll try to update foo next 10 seconds with an interval of 1 second:
private fun updateFoo(fooId: String): Foo {
val updatedFoo = runBlocking {
return@runBlocking withTimeoutOrNull(10L) {
var result: Foo?
do {
result = try {
fooClient.update(fooId)
} catch (ex: Exception) {
null
}
if (result != null) {
return@withTimeoutOrNull result
}
delay(1L)
} while (result == null)
return@withTimeoutOrNull result
}
}
return updatedFoo ?: throw IllegalStateException(
"Could not update Foo ($fooId) with retries.")
}
The point is that i run coroutine blocked. And ten seconds is a big enough interval, so i want to refactor this method to run coroutine nonBlcking.
I've tried to use launch { ... } instead of runBlocking, but i get Job as a result, but that's not what i want, i guess. Could anyone help me to write it correctly???
updateFooto be non-blocking, orfooClient.update? Also, isfooClient.updatea suspending function? - Raman