From the android arch docs... trying to understand...
# compose
c
From the android arch docs... trying to understand something
Copy code
...
        LaunchedEffect(userMessage) {
            snackbarHostState.showSnackbar(userMessage.message)
            // Once the message is displayed and dismissed, notify the ViewModel.
            viewModel.userMessageShown(userMessage.id)
        }
...
Is showSnackbar a synchrnous call? i.e. userMessageShown() will be called ONLY after showSnackbar is done/dismissed?
h
No, showSnackbar is suspend function. It works because LaunchedEffect gets lamdba with coroutine scope, so you can call suspend functions in LaunchedEffect
And yes, userMessageShown will be called only after showSnackbar completes
☝️ 1
a
Or to be a bit more nitpicky, showSnackbar is synchronous but non-blocking. It won't return until the snackbar it shows is dealt with by the user, but it doesn't block the calling thread, it suspends instead.
c
And yes, userMessageShown will be called only after showSnackbar completes
Thanks. I guess I was seeing it more of an "async" thing like toast which is a fire and forget sort of operation. so in this case I'm 100% "acknowledging" that the message was shown ONLY after it has been dismissed. Cool. That's a really big yet subtle point that the comment is making IMO and I wanted to make sure I understood it. This means that on rotation, the snackbar will still be shown if it hasn't yet been dismissed (from what I can understand) which is a good thing.
u
Not sure, but as a suspend function I would expect it to be destroyed when the scope is canceled
c
But it would show the message again because its still in the "queue" so to speak?
h
Yep, if it won't be dismissed and showSnackbar will be cancelled, it will be called again. If showSnackbar will be completed, then dismiss will be called anyway, because it's blocking and cannot be cancelled
c
Very cool. Thanks everyone for teaching me!