Leon johnson

05/13/2020, 7:48 PM
Hey guys I really need help please I am using Kotlin to create an Android App. The problem I am having is I’m trying to use a ViewModel to create an user in Firebase. After the user is created, something should be returned to the UI Fragment to let the fragment know to move forward to the next line of execution. The trouble I’m having is Firebase operations are done asynchronously, so there’s no way for the front end to know the operation is done executing in the view model. Is there a way to create a callback or fix this?


05/13/2020, 8:07 PM
hi leon, i’d recommend looking at livedata for this, that’s typically how data is passed from the view model to the fragment:

Leon johnson

05/13/2020, 8:12 PM
But my fragment is using a view model provider to call the “createUser” function. The create user function is the firebase authentication “create user with email and password” method. So this is happening asynchronously in the view model, while my fragment is moving to the next line of command.
Will live data help this?


05/13/2020, 8:13 PM
Yes, that's the point. Please go read the overview
You won't have your fragment move to the next step until it is told that the firebase thingy is done
👍 1


05/13/2020, 8:33 PM
Another alternative is to utilize the Navigation architecture component. Your Fragment would display a progress indicator to let the user know the Firebase call has begun. It would then call the view model, and inside the viewmodel (when the asynchronous process is complete) you would call the navigation controller to ask it to move you to the next screen. It’s the job of Google’s navigation controller to switch which fragment is displayed, keeping the view code simple, and viewmodel free of android specific classes / Contexts.

Leon johnson

05/13/2020, 8:40 PM
I think that’s a great alternative Paul thanks for that. I was actually working on that now
All i needed was an observer!!! thank you guys so much!!!!!!