class MyClass {
val myInt = mutableStateOf(0)
fun run(){
for (i in 0..10){
myInt.value = i
Thread.sleep(500)
}
}
}
what am I missing?
a
Arsen
03/19/2022, 2:56 PM
Your code is blocking. You can use one of the "Side-Effects API", e.g. produceState:
Copy code
val stateOfInt: State<Int> = produceState(0) {
var result = withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
delay(1000)
1
}
value = result
result = withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
delay(1000)
2
}
value = result
}
P.S. example above is "serial", second block will start when first one finish, to make it "parallel" wrap each block with "launch { }"
j
John Aoussou
03/19/2022, 8:58 PM
Does it mean I have to turn the run() function into a composable function?