Lilly
06/12/2020, 9:28 PMScaffold(bodyContent = { // code here is called twice })
is called twice? I'm using dev13 but same problem with dev12. I can't test the other versions because my project fails otherwise.matvei
06/13/2020, 10:39 AMbodyContent
slot.
This is go away at some point later when we will have lazy subcomposition and we will get sizes synchronously.
Can you share the usecase where the number of calls of your composable lambda matters to you?Lilly
06/13/2020, 3:28 PM@Composable
fun ScannerScreenContent(
onItemAction: () -> Unit
): @Composable() (Modifier) -> Unit = { modifier ->
val viewModel = ScannerViewModelAmbient.current
val modelList = modelListOf<BluetoothDeviceWrapper>()
val state by viewModel.device.observeAsState() // prevents preview from working
state?.let { device ->
modelList.add(device)
modelList.forEach {
Log.d("Items", it.bluetoothDevice.name)
}
}
AdapterModelListComponent(modelList, modifier, onItemAction)
}
But one of the users explained me that I should use remember
and launchInComposition
like so:
@Composable
fun ScannerScreenContent(
onItemAction: () -> Unit
): @Composable() (Modifier) -> Unit = { modifier ->
val viewModel = ScannerViewModelAmbient.current
val modelList = remember { modelListOf<BluetoothDeviceWrapper>() }
launchInComposition(viewModel.device) {
viewModel.device.asFlow().collect { device ->
modelList.forEach {
if (it.bluetoothDevice.address == device.bluetoothDevice.address) return@collect
}
modelList.add(device)
Log.d("Found", "size: ${modelList.size}")
}
}
AdapterModelListComponent(modelList, modifier, onItemAction)
}
matvei
06/14/2020, 4:54 PMScaffold
component, so I'm happy to answer any questions or discuss feature requests around it 🙂
Yeah, I think you figures it out, nice! With proper memoization via remember
, state
, onCommit
, launchInComposition
and other tools it shouldn't matter how many times your @Composable function is being invoked. Broadly speaking, the big application and/or in reusable components its almost impossible to know how many times any particular components in the code will be recomposed.Lilly
06/15/2020, 11:28 AM