Dear Community, I need your help! I have a ViewMod...
# android
v
Dear Community, I need your help! I have a ViewModel where I have 2 methods both are calling
suspend
methods and returning a
Double
result. How I can organise the code so
calculateResult()
method is called ONLY when
resultDoSomething1
and
resultDoSomething2
are received from the server. (without booleans)
Copy code
val resultDoSomething1: Double = 0.0
val resultDoSomething2: Double = 0.0

fun doSowmthing1() {
    iewModelScope.launch {
        resultDoSomething1 = downloadDataFromServer1()
    }
}

fun doSowmthing2(){
    iewModelScope.launch {
        resultDoSomething2 = downloadDataFromServer2()
    }
}

fun calculateResult() {
     val res = resultDoSomething1 + resultDoSomething2
}
n
This might be possible by handling events with Channel.

https://www.youtube.com/watch?v=6v8iJDJdtMc

when a particular event is received, give a call to calculateResult()
e
Launch a single coroutine with 2 async operations (downloadData1 and downloadData2) and await for their result.
✔️ 1