https://kotlinlang.org logo
#coroutines
Title
# coroutines
f

Florian

10/22/2020, 5:18 PM
Where would you put a register/signUp method that needs to execute some extra asynchronous work after the signUp process itself is finished? When I put it into a SignUpViewModel that is scoped to SignUpFragment the viewmodelScope is canceled too early (because as soon as we signed up, we leave this fragment).
b

bezrukov

10/22/2020, 5:20 PM
Injecteable ApplicationScope
1
Another options: GlobalScope/withContext(NonCancellable)
f

Florian

10/22/2020, 5:30 PM
Thank you. However, doing this on the fragment level doesn't seem to work well with Snackbars that I want to show in reaction to the login/register process. The fragment is popped from the backstack too early. Should I put all of this into an activity-scoped viewmodel instead (I share the activity between the login flow and the rest of the app)
s

Shawn Witte

10/22/2020, 10:02 PM
Activity-scoped VM is a good solution for these kinds of things. You might even just move the entire login call to that viewmodel so its scope lives longer (although, not as long as an application level scope).
f

Florian

10/22/2020, 10:44 PM
Thanks. Right now I have login + register + logout in the shared activity-scoped ViewModel (I only have single activity throughout the whole app). But this also means I need to keep the loading state of both the login and the signup fragment there. Do you think that's fine?
And this activity-scoped VM pretty much lives as long as the application
s

Shawn Witte

10/23/2020, 11:36 PM
I think that should be fine. Loading state is usually a UI concern, so I like to set loading state around a
Deferred
returned by an
async
to handle loading state and then the `login`/`signUp`/etc. functions should just return the
User
or
userId
or whatever it is that your app needs from them.
f

Florian

10/24/2020, 8:02 AM
alright, thank you very much