In Android, I rather frequently need to get results from more than one service that utilizes an asynchronous callback pattern and I want to know if there's a better way to handle this pattern than what I'm doing.
Right now the way I handle it goes something like this: I call both services and pass them my callbacks. In the callback for service A, I check and see if I have a cached result from service B. If I do, then I do whatever I need to do with them. If I don't, I cache the result from service A. I do the same in the callback for service B.
So as an example, I have an activity that displays the user's current location on a map. The map loads asynchronously and calls back to onMapReady
. The request for location services calls back to onConnected
. In onMapReady
, I check to see if I have location services. If I do, I set the location on the map to the user's last location. If I don't, I set the location to a default location and cache the map. In onConnected
, I check and see if I have the map. If I do, I set the location on the map to the user's last location. If I don't, I cache the last location.
This pattern works well enough when there are only two services involved, but I can envision scenarios in which I might have three or more, and it seems clumsy. Is there another way I should be handling this?