
I do know that you just already confronted this state of affairs:
The consumer opens a display, and the applying does a community name to retrieve some knowledge to fill the display. However wait… one thing bizarre occurred and the decision failed. I do know that you’re a good developer and handled this exception and on this case, the applying would not crash, it simply exhibits a ravishing error display… “Ops… one thing went improper”. You might be utilizing Kotlin flows and determined to place a “Strive once more” button on the error display. However… how one can restart the movement?
Your view mannequin will look roughly like this:
Right here I made 2 important issues
- I created sealed courses to signify every display state (you’ll be able to learn a bit extra about this method here ).
- I uncovered a Circulate that fetch the shop checklist and wrap it in a
UiState
class.
This method is excellent as a result of the movement will begin as quickly because it will get an observer as you’ll be able to see within the instance under. The instance is made with Jetpack Compose nevertheless it can be used with the common View framework.
Nevertheless, this method would not present us with a simple technique to restart the movement if one thing went improper on the primary attempt. The Circulate framework has a technique to arrange a retry behaviour however this occurs mechanically, not by the consumer command. You possibly can see extra concerning the built-in method here.
I spent a while enthusiastic about tips on how to do it. Learn some articles and reached on the next implementation:
The purpose of this implementation is to wrap the primary movement on an exterior movement that begins with a MutableStateFlow
. This manner, we are able to use the occasion of the state altering to begin the primary movement once more.
To make it extra handy, I additionally create a category referred to as RetryTrigger
to manage when the movement ought to begin once more and maintain the states: RETRYING
and IDLE
.
The state RETRYING
implies that the movement ought to begin. The preliminary state is RETRYING
as a result of we want the primary name to run usually. After the primary name, the state is modified to IDLE
(line 12).
The state IDLE
implies that the movement was already began and returned the primary worth (it could possibly be successful or an error response). We’re filtering the MutableStateFlow
to begin the primary movement solely when the state is RETRYING
(line 10).
Final however not least we now have the tactic retry()
on RetryTrigger
. It set the mutable state to RETRYING
once more and the primary movement is began once more.
On this new implementation we now have the next:
- I created an occasion of
RetryTrigger
and handed it as the primary argument on the retryable movement creation technique. - The principle movement was wrapped in a retryable movement. I made a decision to move this movement inside a lambda. I might even make this technique inline, however these are implementation choices, and is as much as you to resolve what suits higher to your venture.
- Now there’s a
tryAgain()
technique that callsretryTrigger.retry()
. This technique needs to be referred to as when the consumer presses the “attempt once more” button on the error display.