如何避免链接数AsyncTask的电话? [英] How to avoid chaining several AsyncTask calls?
问题描述
我不得不多次调用Web服务,但每一步都使用从previous一步的值,所以现在我有一个巨大AsyncTasks链:每个AsyncTask的是在onPostExecute)的执行( AsyncTask的在previous步骤。这是非常非常难看,而且很难修改。我怎样才能避免这种情况?我希望把每一步一个单独的函数:
I have to make several calls to a web service, but each step uses the values from the previous step, so right now I have a huge chain of AsyncTasks: each AsyncTask is executed in the onPostExecute() of the AsyncTask of the previous step. This is very very ugly, and very hard to modify. How can I avoid this? I would like to put each step in a separate function:
int step5() //this function is on the main UI thread
{
return getStep5ValuesFromWebService() + valuesFromPreviousSteps;
}
int getStep5ValuesFromWebService()
{
//do work on new thread
//GetValueFromService(); <-- this is the function that returns an int from the web service, but it has to be called from another thread
}
如何调GetValueFromService(),从而使步骤5()函数返回GetValueFromService()函数计算出的值α
How to call GetValueFromService() so that the step5() function returns the value calculated in the GetValueFromService() function?
推荐答案
可以这样做:
private class MyAsync extends AsyncTask<String, Integer, Long> {
protected Long doInBackground(String... symbols) {
step1();
step2();
step3();
step4();
step5();
}
private void step1(){}
private void step2(){}
private void step3(){}
private void step4(){}
private void step5(){}
protected void onProgressUpdate(Integer... progress) {
}
protected void onPostExecute(Long result) {
}
}
这篇关于如何避免链接数AsyncTask的电话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!