Hi everyone, I want to measure how long a Composab...
# compose-android
a
Hi everyone, I want to measure how long a Composable takes to render, this should include the 3 phases (Composition, Layout and Drawing), I am not sure how to know that the Drawing phase has completed, Hi everyone, I need to measure how much time a Composable takes to render , this should include the 3 phases (Composition, Layout and Drawing), using
LaunchedEffect
seems to work, but is it guaranteed that it will be always called after the Drawing phase? The doc on
LaunchedEffect
says it will launch a job when it enters the Composition (so it should work in the first phase, right?), this gave me the impression that it doesn’t wait until the Drawing phase, but in reality it actually does for some reason, but not sure if this is guaranteed in all cases.
Copy code
@Composable fun Example() {
     val startTimestamp = System.currentTimeMillis()
     Text("Hi)
     LaunchedEffect(Unit) {
        val endTimestamp = System.currentTimeMillis()
        reportTime(endTimestamp - startTimestamp)
   }
}
m
Try adding a
drawBehind
modifier to the text, The
drawBehind
lambda should be executed on the Drawing phase
a
Thanks for the suggestion, yes indeed this works, but the lambda to
drawBehind
will always be called each time the
Text
Composable is drawn (could be too much specially in animation for example), and I need to report the time only once, so I am trying to see if
LaunchedEffect
will provide similar behaviour without the overhead.
m
What about
drawWithCache
it's going to be called again only if there is changes on the measured layout
a
Good idea but it still has a part that will be called all the time?
Copy code
.drawWithCache {
        // cached
        onDrawWithContent {
          // called every time it's getting drawn
        }
    }
I think what I wonder about is, is it guaranteed that
LaunchedEffect(state)
will wait until this
state
is indeed drawn?