Open TheFedex87 opened 3 years ago
This is the approach I tried to use: in the repository layer, I declared a StateFlow variable which represents the state of the fetchUsers method. In this way before trigger a users fetch I can directly observe the state of this variable and react to it. If a loading is in progress I can easily detect it from anywhere and show a progress bar. When network call is end, the variable is updated and since I'm collecting it I change the state of my UI. Is this a proper way to handle this situation?
I'm facing a problem adopting the mechanism of network bound resource. To explain my problem I'll take the UserRepository class that implements the network bound resource abstract class. I have a fragment that needs to access user data in order to display a list of users. Figure this is the first time I open the application and figure the logic of fetching is: fetch from the network if data is null. Now since this is the first time I run my app, data fetch is started. Connection is not very good so the fetch needs time. Now figure I open another fragment that, again, uses the same data: the list of users. The ViewModel of this second fragment call again UserRepository. getUsers...Since the fetch of data of the first fragment is still in progress, again the shouldFetch return true (local data is still null) a new data fetch is triggered. The problem is: I don't know the state of the previous call at the same source of data. How can be handled this using the mechanism of network bound resource?