https://kotlinlang.org logo
#compose
Title
# compose
m

Mehdi Haghgoo

09/07/2020, 5:48 PM
When a composable function is invoked, the invocation might occur on a different thread from the caller. That means code that modifies variables in a composable lambda should be avoided–both because such code is not thread-safe, and because it is an impermissible side-effect of the composable lambda. -link
Parallel execution in Compose is very new to me. Could someone tell me please how I can modify variables if I am not allowed to do it in a Composable lamda? Also, what does thread-safety mean in the above paragraph?
m

manueldidonna

09/07/2020, 5:59 PM
This means that you can't make assumptions about how many times a composable will be invoked. Modify a variable is considered as a side effect of the composition and you can do it using
onCommit
or
launchInComposition
This thread has been very useful to me https://kotlinlang.slack.com/archives/CJLTWPH7S/p1591653158447300?thread_ts=1591653158.447300&cid=CJLTWPH7S
👍 2
s

sante

09/07/2020, 8:01 PM
you can also use remember inside composables so that the values are not calculated in every composition, just when you need them
3 Views