hawklike
08/09/2023, 6:37 AMremember
is a composable function, it means we have no guaranteed order in which they will be executed? Or remember
functions run sequentially?
@Composable
private fun Foo() {
val context = LocalContext.current
val cameraManager = remember {
context.getSystemService(Context.CAMERA_SERVICE) as? CameraManager
}
val backCamera = remember(cameraManager) { <------- are we sure the cameraManager is initialized?
cameraManager?.cameraIdList?.firstOrNull()
}
...
}
Timo Drick
08/09/2023, 9:23 AMhawklike
08/09/2023, 10:32 AMIf a composable function contains calls to other composable functions, those functions might run in any order.The
Foo
is a regular composable function that calls other composable functions, the remember
functions. Why do you think it executes sequentially?Timo Drick
08/09/2023, 1:28 PMhawklike
08/09/2023, 1:56 PMTimo Drick
08/09/2023, 1:58 PMTimo Drick
08/09/2023, 2:00 PMTimo Drick
08/09/2023, 2:01 PMLoney Chou
08/09/2023, 4:49 PMhawklike
08/09/2023, 7:15 PMdewildte
08/09/2023, 8:25 PM@Composable
function can be chosen to be invoked by the runtime at arbitrary points in time.
But once a function is invoked then it’s body will be executed sequentially.
There is no way to get around that.
That’s what the annotation is for, it allows the compiler to see all the composable functions declared in the code
prior to runtime.Albert Chang
08/15/2023, 2:37 PM