I am stuck on a problem, deriving from the following facts:
- Android Main thread should not be blocked
- I need to perform dependent network calls (for example a login request onSuccess of an authentication request)
- onSuccess of the login request I need to update the UI (like navigating from an Activity to another).
I could solve this issue this just by nesting the code in Volley directly in my Activity and having this pattern:
authenticationRequest() {
onSuccess(): loginRequest() {
onSuccess(): navigateTo(someActivity)
onError(): // display some error
}
onError(): // display some error
}
And this could also be an exceptional case since it occurs just once in the application.
However, you can guess that there may be some other cases of request B getting triggered on response of A, but I could be interested in using only A and not B in some other scenarios.
That's the reason why I think it would be great to isolate methods independently and let me compose them only when necessary.
The problem is that the asynchronous nature of these functions doesn't let me avoid the dependency. Instead, it seems like dependency implies synchronous requests or duplicated nested code everywhere. I am never able to have in my Activity something like:
authenticationRequest()
// wait for the result without freezing main thread
loginRequest()
// wait for the result without freezing main thread
navigateTo(someActivity)
The closest solution I was getting was a little verbose and a little dirty:
create observable live data and do postValue(response) where response is the value onSuccess or onError; consequently, in the Activity I had something like:
authenticationRequest()
authenticationResponse.observe(this, {
loginRequest()
loginResponse.observe(this, {
navigateTo(someActivity)
})
})
but this solution overcomplicates the management of observable data getting triggered twice when I go back and forward in some Activities.
Do you have any better suggestion?
Edit Note: the order of the functions execution matters.
Your question is pretty unfocused. After reading both your question and comments you seem to have several concerns regarding synchronous/asynchronous execution. I won't answer all your concerns, but I can at least give you some insights on this topic.
A,BandCrequests, whereBuses data fromAandCuses data fromB, then we invoke them sequentially. If they are independent, we invoke them concurrently and join on them all.val a = requestA(); val b = requestB(a); requestC(b);or:launch { requestA() }; launch { requestB() }; launch { requestC() }- whichever we need at the time. The performance is always as we code asynchronously.