r2reg
r2reg

Reputation: 37

Does view is rendered concurrently in Android?

I write a compose function like that:

ScrollableColumn {
    for (x in 0..10000) {
        ClickCounter(viewModel)
    }
}

var num = 0

@Composable
fun ClickCounter(viewModel: MyViewModel) {
    Log.e("TAG", "Composing $num-th btn")
    num++
    val count: Int by viewModel.count.observeAsState(0)
    Button(onClick = {
        viewModel.count.value = viewModel.count.value?.plus(1)
    }) {
        Text("I've been clicked $count times")
    }
}

I this case the log shows that the buttons are rendered sequentially because the for loop is executed sequential, and I guess that does there are some cases that compose functions are rendered simultaneously? And does that is relative to View in android is not thread-safe?

Upvotes: 0

Views: 92

Answers (1)

Rafsanjani
Rafsanjani

Reputation: 5463

Yes, composables are rendered sequentially and they are completely thread-safe. In fact, JetPack compose is single threaded so atomicity is assured. Whenever a state variable is modified, the code block completely executes to complettion before recomposition occur

Upvotes: 1

Related Questions