• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Android - AsyncTask onProgressUpdate called but not “as expected”?

Using an Async task as follows:
private class ProcessFileTask extends AsyncTask<Void, byte[], Void>{
String fileName;
BufferedReader bufferedReader;
String[] splitData;
byte[] currentArray;
ByteBuffer bb;

public ProcessFileTask(String fileName) {
this.fileName = fileName;
currentArray = new byte[19];
}

@override
protected Void doInBackground(Void... params) {
try {
bufferedReader = new BufferedReader(new FileReader(fileName));
splitData = bufferedReader.readLine().split(" ");
for (int i = 0; i < splitData.length; i += 19) {
for (int j = 0; j < 19; j++) {
currentArray[j] = Byte.parseByte(splitData[i+j]);

}
publishProgress(currentArray);
}

}
catch (Exception e){
Log.e("Exception occured", e.toString());
}
return null;
}



@override
protected void onProgressUpdate(byte[]... values) {
super.onProgressUpdate(values);
bb = ByteBuffer.allocate(20);
bb.order(ByteOrder.LITTLE_ENDIAN);
bb.put(values[0], 1, 18);
ChannelOne.updateGraph(bb.getShort(0), bb.getShort(2), bb.getShort(4));
ChannelTwo.updateGraph(bb.getShort(12), bb.getShort(14), bb.getShort(16));

}
}​

At this part of execution:

for (int i = 0; i < splitData.length; i += 19) {
for (int j = 0; j < 19; j++) {
currentArray[j] = Byte.parseByte(splitData[i+j]);
}
publishProgress(currentArray);
}
I expect the publishProgress to be called everytime the 'i' loop executes. While that happens(which was verified by Logging data), the code in onProgressUpdate() does not get executed properly(which is evident because the result of updateGraph can be seen only after the AsyncTask ends. Also, the end graph is completely mis-formed(the graph shows mostly straight lines while it must be curves).

This strange problem does not occur, if the whole code is run directly on UI Thread and the graph is rendered perfectly. In that case, as you know, the UI gets blocked for a few seconds.

I tried, instead of AsyncTask running the code as an individual thread but the same results persist. Any workarounds can be made? Can a service be used in this case?
 
Your method signature for onProgressUpdate is slightly wrong. You have

Code:
protected void onProgressUpdate(byte[]... values) {

However, your class has the declaration

Code:
private class ProcessFileTask extends AsyncTask<Void, byte[], Void>{

Therefore your onProgressUpdate method should be declared like this

Code:
protected Void onProgressUpdate(byte[]... values) {

In essence, void != Void, they are different types
 
Thanks, but from what I understood, the third Void stood for the return type of the doInBackground(). Anyway, changing the signature of onProgressUpdate() to return a Void gave me an error that said that this method 'clashed' with the corresponding method in AsyncTask class and could not be overridden.
 
Did you verify that publishProgress() is being called when you expect, by setting a breakpoint in your code?
 
Also set a breakpoint in onProgressUpdate() to be sure when that is being called (or not)
 
Have you verified that the values passed to onProgressUpdate() are correct/valid data?
 
Have you verified that the values passed to onProgressUpdate() are correct/valid data?
Yes, they work well if no async task is used, i.e. the same code executes well on the UI Thread. However, since putting such an operation on UI Thread blocks it, I chose to go with AsyncTask. Or, can a Background Service be used?
 
AsyncTask should work.
What does updateGraph() do, you haven't shown the code. And does it execute successfully from onProgressUpdate(), without errors or exceptions being generated?
 
AsyncTask should work.
What does updateGraph() do, you haven't shown the code. And does it execute successfully from onProgressUpdate(), without errors or exceptions being generated?

The updateGraph() is a method that adds points to a TimeSeries(from achartengine-1.1.0). The updateGraph() itself poses no direct problem, since it works in another place on the app. And no exception is thrown, it just seems like no graph updates happen from the onProgressUpdate() itself, and changes in graph can be seen only after the AsyncTask completes. The final graph though is mis-formed.
 
As your graph is misformed, that suggests something wrong with the data being passed to the plot.

I would set a breakpoint in the onProgressUpdate() method, look at the values being passed in, and trace the execution of the code into the updateGraph() method. Just because a piece of code works in one context, doesn't mean it'll work in another. Don't make assumptions. Check everything.
 
Back
Top Bottom