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

Colton Idle

02/02/2022, 5:59 AM
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

heckfyxe

02/02/2022, 6:06 AM
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

Adam Powell

02/02/2022, 2:19 PM
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

Colton Idle

02/02/2022, 7:29 PM
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

uli

02/02/2022, 7:31 PM
Not sure, but as a suspend function I would expect it to be destroyed when the scope is canceled
c

Colton Idle

02/02/2022, 7:32 PM
But it would show the message again because its still in the "queue" so to speak?
h

heckfyxe

02/02/2022, 7:44 PM
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

Colton Idle

02/02/2022, 8:36 PM
Very cool. Thanks everyone for teaching me!