0

Suppose I have a situation where an API response fills my Musician model with information specific to that musician. There is another API response that, given a musician name, describes related artists.

I want to launch a detail activity showing the musician's name, members, genre, albums(API response A) along with related artists(API response B).

An asynchronous network call will provide a better user experience, but since launching the detail activity is dependent on 2 different api calls, is there a better solution than having API call be asynch and API call B be synch(blocking A and the UI)?

If I make both asynchronous, API call B could be running while the activity changes, stopping it(since an asynctask is destroyed when the activity changes). This would result in missing data in the detail activity.

HukeLau_DABA
  • 2,384
  • 6
  • 33
  • 51

1 Answers1

0

In a simple case:

  1. Start the detail activity
  2. Execute both requests asynchronously while presenting the user with a loading UX
  3. Once is retrieved, update the UI accordingly
    • You can change the layout of an Activity at any time at runtime without recreating it.
    • You can either update the UI immediately with "Response A" and then again with "Response B" or wait for both responses to return and then update the UI; the former is preferable.

Network operations should never block the UI or the user. Displaying loading spinners and such is perfectly acceptable and is used all the time in Google's own apps.

Jeffrey Mixon
  • 12,846
  • 4
  • 32
  • 55