Created
May 22, 2022 23:11
-
-
Save vinaygaba/67c9fda114bfc7bf7da3ca3bb3fcb59f to your computer and use it in GitHub Desktop.
Useful for printing log statements when a composable function is being recomposed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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