In an Android app, let’s say there is a settings screen and the settings are stored on a server.
Whenever the user changes settings a state-modifying HTTP call (POST/PUT/PATCH/DELETE) is made.
What is the best way to implement this?
Here are the options I see:
- “coroutines-viewModelScope”: GET calls can be executed using Kotlin Coroutines by dispatching them to the IO thread and scoping them to a viewModelScope. This way, they are cancelled when the screen is left and it doesn’t matter if the GET call reached the server or not.
State-modifying calls could be handled in the same way as GET calls but there is a critical scenario: If the user wants to leave the screen before the server returned a result (e.g. due to slow internet connection) the coroutine is cancelled and we don’t know if the server received the request. This could be mitigated by showing a dialog and asking the user if she really wants to leave the screen. - “coroutines-app scope”: Use a coroutine to run the request but scope it to the app scope / Global Scope. The open question here: If the user has already left the screen before the request has finished, should she be notified if the request failed and even if the request was successful? And if so in which form – a toast?
- “persistent work”: While approach 2 requires the app to be running, the request could even be executed if the app is being closed. The same question as 2 remains: How to update the user. Also: Would you use WorkManager or ForegroundService?
- “local storage and server sync”: Save settings to a local database or shared preferences so the UI can be updated instantly. Rely on a background server sync that the settings are synced with the server eventually. Would work manager be used to implement the server sync? Which sync intervalls make sense? If the same user uses multiple clients conflicts could arise – does this need special attention?
What is the best option in your opinion? Does your pick depend on considerations like available budget, etc.?
In case of option 2 and 3: How would you give UI feedback?