开发者

onProgressUpdate misses some publishes

Is it possible that some calls publishProgress missed by onProgressUpdate? I mean is it possible there may be one transfer miss between doInBackground and onProgressUpdate callbacks with using publis开发者_JAVA百科hProgress call. Because I see that.

class DoSomething extends AsyncTask Void, String, Void {
  String[] S = new String[] {"a", "b", "c", "d"};

  void doInBackground(Void... ps) {

    for(String s : S) {      
      publishProgress(s);
    }
   }

  void onProgressUpdate(String... vs) {

   Log.d("", vs[0]);
 }

What i am encountering that resulting

a b b d

what happening to c?

Note: This is just illustration of my application, and this happens sometimes(not at all run), i could not write all the codes here because its too complicated. But in summary this is happening.

So any ideas?


Ok. I dug deep and found the answer that: message transmission between doInBackground and onProgressUpdate is not serial that is asynchronous (from the base class name). Any call in doInBackground with publishProgress(xx) will eventually reach to onProgressUpdate but not one-to-one.
For example, very dummy demonstration:

doInBackgroud() {
String s;
 for(i=1 to 10) {
   s = i.toString();
   publishProgress(s);
  }
}

onProgressUpdate(String par) {
Log.d(par);
}

can be result as: 1 2 2 3 4 5 5 5 6 7
What tha ? Dont worry, we sending the local variable s (in doinback.) by reference and by the time onProgressUpdate invoked and it is logging its value, there is probability that doInBackground is changing the value of s. That results unexpected value in the onProgressUpdate. But all publishProgress calls invoke the onProgressUpdate method.
If we wrote:

doInBackgroud() {

 for(i=1 to 10) {
  String s = i.toString();
   publishProgress(s);
  }
}

onProgressUpdate(String par) {
Log.d(par);
}

this time under normal conditions, result must be : 1 2 3 4 5 6 7 8 9 10
and this is what we expect, isn't it?
if there is better idea, i would like to hear that
And any comments will be well-welcomed.
Note: Maybe too simple case, but saved my week.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜