Hi all, I was wondering about something: If I use UseCases to do things in my app, and I have lets say a screen and I can load an item, lets say a user account, into the screen using the id of the user. I would like to observe the user by its id from the current value in the repository, and of course any changes in the user will be propagated in my flow.
So I have a ObserveUserUseCase to handle this for me.
My question: If lets say this user is not existing yet in my repository and I need to fetch it from the backend, would you put that logic into the
a) The ObserveUserUseCase (if the repository returns null/not existing it fetches the user)
b) In the repository, observing on the user might trigger a backend call in this case automatically
c) Make the ObserveUserUseCase return something to show its not existing in the repository (like NullPointer) to my ViewModel and call a separate FetchUserUseCase to load the user
07/19/2020, 12:10 PM
I'd definitely say C. Your usecase should perform a single task. Create a sealed class that has a dedicated state to define NotFound and have your VM call a 2nd usecase with it's own function.
07/19/2020, 12:13 PM
@brandonmcansh I was thinking about b as well, as it makes the usecases and viewmodel more agnostic as to where the data comes from, but otherwise c makes sense as well. (A forced refresh on the other hand would indeed be a separate usecase)
07/19/2020, 1:08 PM
Yeah either one could be applicable and with B you could still use a 2nd usecase and provide to ObserveUseCase to fulfill that request