onProgressUpdate 错过了一些发布
是否有可能 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的。我深入挖掘,发现答案是:
doInBackground
和onProgressUpdate
之间的消息传输不是异步的串行(从基类名称)。doInBackground
中使用publishProgress(xx)
的任何调用最终都会到达onProgressUpdate
,但不是一对一的。例如,非常虚拟的演示:
结果可以为: 1 2 2 3 4 5 5 5 6 7
什么?
别担心,我们通过引用发送局部变量 s(在 doinback 中),并且当调用
onProgressUpdate
并记录其值时,doInBackground
有可能正在更改s 的值。这会导致onProgressUpdate
出现意外的值。但所有的publishProgress调用都会调用onProgressUpdate
方法。如果我们写:
这次在正常情况下,结果一定是: 1 2 3 4 5 6 7 8 9 10
这就是我们所期望的,不是吗?
如果有更好的想法,我想听听
任何评论都将受到欢迎。
注意:也许情况太简单,但节省了我的一周。
Ok. I dug deep and found the answer that: message transmission between
doInBackground
andonProgressUpdate
is not serial that is asynchronous (from the base class name). Any call indoInBackground
withpublishProgress(xx)
will eventually reach toonProgressUpdate
but not one-to-one.For example, very dummy demonstration:
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 thatdoInBackground
is changing the value of s. That results unexpected value in theonProgressUpdate
. But all publishProgress calls invoke theonProgressUpdate
method.If we wrote:
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.