onProgressUpdate 错过了一些发布

发布于 2024-11-28 09:08:02 字数 747 浏览 0 评论 0原文

是否有可能 onProgressUpdate 错过了一些调用publishProgress?我的意思是,使用publishProgress 调用时,doInBackground 和onProgressUpdate 回调之间是否可能存在一次传输丢失。因为我看到了这一点。

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

我遇到了什么结果

abbd


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?

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

友谊不毕业 2024-12-05 09:08:02

好的。我深入挖掘,发现答案是:doInBackgroundonProgressUpdate之间的消息传输不是异步的串行(从基类名称)。 doInBackground 中使用 publishProgress(xx) 的任何调用最终都会到达 onProgressUpdate,但不是一对一的。
例如,非常虚拟的演示:

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
什么?
别担心,我们通过引用发送局部变量 s(在 doinback 中),并且当调用 onProgressUpdate 并记录其值时,doInBackground 有可能正在更改s 的值。这会导致 onProgressUpdate 出现意外的值。但所有的publishProgress调用都会调用onProgressUpdate方法。
如果我们写:

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
这就是我们所期望的,不是吗?
如果有更好的想法,我想听听
任何评论都将受到欢迎。
注意:也许情况太简单,但节省了我的一周。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文