2

I have an app where I connect to a web service to retrieve data. I'm using AsyncTask to do this. Here is a piece of the code I'm using in doInBackground:

publishProgress(user.dbsCode() + ": Retrieving Standard Comments");
if (!_sync.isCancelled()) {
    result = _dbFiller.fillStandardCommentsTable();
} else {
    return null;
}

When I test this in my emulator it seems to work fine. isCancelled gets set & onCancelled() is called. When I test using my Galaxy Tablet (running 3.2... my project was created using 2.2), it's hit or miss if isCancelled ever gets set.

This process can take a while depending on how much data a particular user is downloading. First thing I do is download a list (of what we call) "jobs" for the user. I then call separate web services to download the different pieces for a "job". Each step includes the above code where I'm checking if my sync task is cancelled. I'm really lost. Any helpful comments are appreciated. Even just a direction. I've done so much reading about AsyncTasks that my head is spinning.

dandan78
  • 13,328
  • 13
  • 64
  • 78
Chris
  • 91
  • 2
  • 7

1 Answers1

0

Under what circumstances are you canceling the download?

Since you don't really get to control the scheduler, it may be that your tasks are running to completion before you are able to cancel them. Since the real hardware is probably faster than your emulator, that seems possible.

Canceling an AsyncTask that's already finished doesn't do anything, if I recall. Are you sure your tasks aren't complete?

Argyle
  • 3,324
  • 25
  • 44
  • I'm actually calling multiple web services from this one task. Before calling each, I check if the task is cancelled. For some reason the emulator will Cancel in the middle of the web service call. The tablet will finish the call before setting "isCancelled". Still not sure why one works differently than the other. To work around the issue, I simply throw another dialog that says "finishing current operation" until the isCancelled flag gets set. – Chris Feb 10 '12 at 21:14
  • @Chris I'm still not sure what your problem is exactly. If the `AsyncTask` completes before testing to see if it's cancelled (from within `doInBackground()`, presumably), then it's completed. The OS doesn't interrupt your code in mid-method call. That's why any long-running code you have needs to call `isCancelled()` to know if it should continue or not. The cancellation happens outside the `AsyncTask`'s thread, so you can't make any assumptions about the order things happen in. – Argyle Feb 10 '12 at 21:33
  • Ok, so lets say I have a web service that returns a 300 records that I'll insert into an ArrayList. I have one method that will start a chain of events (for calling web service, parsing XML, filling list, etc.. all in different classes). With my testing, if I cancel the async, then check the size of my ArrayList, I'll get a random number between 1 & 300. If I cancel on the tablet, the list continues to fill until all records are inserted into the ArrayList. I got a friends phone (running 2.3) & got the same result on his phone as I do w/ the emulator. – Chris Feb 10 '12 at 23:42
  • At this point, I'm mainly just curious about why I'm seeing different behavior on a phone/emulator (running 2.3/2.2) compared to the tablet running 3.2. The issue is handled, but I'm still curious. – Chris Feb 10 '12 at 23:49