Reputation: 6043
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 that way. Eg: If you need quote info for GOOGLE then send a request to server requesting for google's quote info. Next if you need yahoo's then you initiate another http call and so on.
Here is the situation:
My Question:
Note: I guess Android 1.6+ do execute Async tasks in parallel.
This is more of a design question and I would appreciate any help on this.
Thanks in advance
Upvotes: 13
Views: 20508
Reputation: 97
just stabbing in the dark here, but you have:
why not have a variable of public scope in the UI thread say called "finishedTasks", then the same method in each of the 6x return data threads that:
increments finishedTasks
if finishedTasks == 6 then run 1 public method to do the update of the UI
then it would update the UI on all background asychtasks completing.
Upvotes: 1
Reputation: 6043
I found this solution more appropriate to my problem. This link describes a couple of ways of establishing this. 1. ExecutorService 2. ExecutoreService and CountDownLatch
ExecutoreService and CountDownLatch
Upvotes: 1
Reputation: 1340
You could make a AsyncTask pool object that allows you to spoof a 'batch' http call.
General idea is for the Pool to know how many Tasks and pending, and to store the aggregate data of completed calls. Once all are finished, notify the observing Activity and pass back all the data.
You'll have to work out how the AsyncTasks tell the Pool that they are finished. Maybe simply have an implementation of AsyncTask that takes a Pool on its constructor so that the Tasks have a reference to the Pool.
Upvotes: 4
Reputation: 8176
There are many ways you could handle this:
AsyncTask
that aggregates the responses from the network tasks.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.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.
Upvotes: 1