onProgressUpdate错过一些出版 [英] onProgressUpdate misses some publishes

查看:173
本文介绍了onProgressUpdate错过一些出版的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有可能是某些电话publishProgress由on​​ProgressUpdate错过了什么?我的意思是有可能有可能是doInBackground和onProgressUpdate回调之间的一个传递思念使用publishProgress通话。因为我看到了。

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 publishProgress 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]);
 }

我所遇到的导致结果

A B B D

What i am encountering that resulting

a b b d

推荐答案

确定。我深掘,发现答案是:之间的消息传递 doInBackground onProgressUpdate 不是序列是异步的(从基类名称)。在 doInBackground 的任何调用与 publishProgress(XX)将最终达到 onProgressUpdate 但不是一对之一。结果
例如,很假演示:

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);
}

可以是结果为:1 2 2 3 4 5 5 5 6 7结果
塔是什么?
通过参考和 onProgressUpdate 调用,它是记录其价值的时候不要担心,我们发送局部变量s(在doinback),有概率 doInBackground 正在改变s的值。这在 onProgressUpdate 产生意想不到的价值。但是,所有publishProgress调用调用 onProgressUpdate 方法。结果
如果我们写道:

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);
}

这个时候正常情况下,结果必然是:1 2 3 4 5 6 7 8 9 10结果
而这正是我们所期望的,不是吗?结果
如果有更好的主意,我想听到结果
和任何评论将被精心欢迎。结果
注意:也许过于简单的例子,但我保存一周

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.

这篇关于onProgressUpdate错过一些出版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆