How to manage multiple Async Tasks efficiently in Android

后端 未结 4 455

I have scenario where I will have to make six http calls to my server to get the data for six different items. These server calls cant be combined and they are meant to be t

4条回答
  •  醉酒成梦
    2020-12-05 16:27

    There are many ways you could handle this:

    • Another AsyncTask that aggregates the responses from the network tasks.
    • Perform a sendEmptyMessageDelayed() every, say, 500ms to a Handler created by your Activity that refreshes the data that has come in from the network tasks and continues to do so until all of the networking results are dealt with.
    • A Thread that performs the aggregation.

    Were it me, I'd probably go with the Handler. To sum up, have your network tasks store the data in some intermediate storage. Send delayed messages to a handler and, within the handleMessage() check for data in the intermediate storage and post updated results. If there are results outstanding, post the delayed message again.

提交回复
热议问题