简体   繁体   中英

Android time lag in network response

I am getting data for one single database from two different network calls. one response takes 5 seconds and another takes 15 seconds. Now, the condition is I can present data to the user only after having an entire dataset ready in my memory from both the network calls. So, 1) How can I deal with this(15-5 = 10sec.) time lag? 2) I have to have shown some data to the user simultaneously. I can not leave them blank until I'll have the entire dataset ready in memory.

From a UX perspective - you are absolutely right when saying that you should show some data to a user sooner rather than later. You should populate your result list with some data as soon as first request is loaded, and fill it up with additional ones once a second one resolves. You should for certain wanna show a loading indicator while loading is still in progress! There is a good explanation of progress indicators in Material Design Guidelines .

I just noticed that you tagged this question with retrofit and rx-java tags. Using those, it's easy to wait for both requests to finish and write code that will execute afterwards. You may use zip method of RxJava . Zip will take multiple Observables and:

"return an Observable that applies a function of your choosing to the combination of items emitted, in sequence, by two (or more) other Observables, with the results of this function becoming the items emitted by the returned Observable."

Assuming you have two base observable objects (one for each of your two requests), on compleating each of them you fill in your data set (probably RecyclerView's adapter?? ), and after both are complete (zip's observable) you hide a progress loading indicator.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM