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

hawklike

08/09/2023, 6:37 AM
👋 If composable functions can execute in any order and the
remember
is a composable function, it means we have no guaranteed order in which they will be executed? Or
remember
functions run sequentially?
Copy code
@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()
    }

    ...
}
t

Timo Drick

08/09/2023, 9:23 AM
I don't think that you can say this. And inside of you composable function the code is executed sequentially except when you use corotines or other threads.
h

hawklike

08/09/2023, 10:32 AM
Why is that? From docs:
If 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?
t

Timo Drick

08/09/2023, 1:28 PM
Composable functions are executed several times and e.g. the code inside the remember block is only executed once or dependent on the key. But still the code is executed sequentially more or less 🙂. Unfortunately compose is very complex under the hood. So it is difficult to assume general things. But when you assign a variable than this must be executed instantly otherwise the UI would be stuck.
h

hawklike

08/09/2023, 1:56 PM
What it means executed sequentially more or less, please? Can we assume that a composable function prior to other composable functions will always be called before them (if it will be called)?
t

Timo Drick

08/09/2023, 1:58 PM
Yes at the end it is just kotlin code. But things which are inside curly braces coulb be executed later. This is a kotlin convention because this is just the last parameter of a function.
So for your example remember is definitely executed immediately. But the part inside of the curly braces. You never now until you look inside of the function. Because this block is just one parameter
But remember must return some thing. Otherwise this code would not compile.
l

Loney Chou

08/09/2023, 4:49 PM
I think the documentation needs to be more clear about what "in any order" means. From my experience, it actually means "they may be skipped, or executed sequencially". So it's not that in any order, it's just that some of the composable functions will be skipped this time, but executed another time, so it feels like they are not sequencial. It's a special side effect of smart recomposition.
h

hawklike

08/09/2023, 7:15 PM
Thank you.
d

dewildte

08/09/2023, 8:25 PM
Any
@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.
a

Albert Chang

08/15/2023, 2:37 PM
I believe the doc meant all composable functions without return value, which means they don't have data dependencies on each other. Composable functions without return value can potentially run in any order once multithreaded composition is enabled. The doc mention that even if it's not true for now because not assuming execution order (i.e. not relying on side effects) is the best practice, and will reduce the work needed to support multithreaded composition when it's out.
97 Views