I have a suspend method: checkIfAvailable() that returns a Boolean. I want to get that boolean value and then use it in my activity. I try this the following way(not sure if this is how it should be done):
suspend fun checkIfAvailable(year: Int): Boolean = viewModelScope.async{
return@async dao.checkIfAvailable(year)
}.await()
This returns a boolean. The problem is that i can't use that boolean, because the function must be a suspend function as await() must be called inside one.
How do i get the boolean value without blocking the main thread?
I tried it with runBlocking in my activity:
fun something(tag: Int): Boolean {
return runBlocking {
return@runBlocking checkIfAvailable(tag)
}
}
but this crashes or blocks the mainthread.
I just need a way to get the value out of the suspend function and into a variable without blocking the thread.
LiveDataobject if you are usingViewModel, that way you'll be properly notified of a new change when observing that particular livedata (Booleanin this case) - DarShan