Where would you put a register/signUp method that ...
# coroutines
f
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
Injecteable ApplicationScope
1
Another options: GlobalScope/withContext(NonCancellable)
f
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
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
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
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
alright, thank you very much