Skip to content

Instantly share code, notes, and snippets.

@vinaygaba
Created May 22, 2022 23:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vinaygaba/67c9fda114bfc7bf7da3ca3bb3fcb59f to your computer and use it in GitHub Desktop.
Save vinaygaba/67c9fda114bfc7bf7da3ca3bb3fcb59f to your computer and use it in GitHub Desktop.
Useful for printing log statements when a composable function is being recomposed
// Full credit for this snippet goes to Sean McQuillan - https://twitter.com/objcode
class Ref(var value: Int)
// Note the inline function below which ensures that this function is essentially
// copied at the call site to ensure that its logging only recompositions from the
// original call site.
@Composable
inline fun LogCompositions(tag: String, msg: String) {
if (BuildConfig.DEBUG) {
val ref = remember { Ref(0) }
SideEffect { ref.value++ }
Log.d(tag, "Compositions: $msg ${ref.value}")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment