uservoice / uservoice-android-sdk

UserVoice Android SDK
https://www.uservoice.com/mobile/
MIT License
116 stars 105 forks source link

babayagaTask stalls other background task #203

Open fefe982 opened 8 years ago

fefe982 commented 8 years ago

BabayagaTask is started with AsyncTask.execute().

Sometime, when the network condition is not good, the connect operation can take quite a lot of time. This stalls other background task on some versions of android.

On some versions of android (and the most recent versions of android), AsyncTask.execute() will only use one background task. This makes all other background task wait for it to finish.

The android document for AsyncTask.execute() says:

Note: this function schedules the task on a queue for a single background thread or pool of threads depending on the platform version. When first introduced, AsyncTasks were executed serially on a single background thread. Starting with DONUT, this was changed to a pool of threads allowing multiple tasks to operate in parallel. Starting HONEYCOMB, tasks are back to being executed on a single thread to avoid common application errors caused by parallel execution. If you truly want parallel execution, you can use the executeOnExecutor(Executor, Params...) version of this method with THREAD_POOL_EXECUTOR; however, see commentary there for warnings on its use.

Any workaround for this?

dluco commented 8 years ago

If you always want your background task to run parallel to the BabayagaTask, just use:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    yourTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);
} else {
    yourTask.execute(params);
}

If your minSdkVersion is 11+, you don't need the else-block.