This is a custom wrapper of Android AsyncTask
that I use in my projects.
It has more convenient fluent API for managing async operations and supports flexible exception handling.
You can omit methods that you do not need, like onBefore(...)
or onAfter(...)
.
Also you do not need to inherit AsyncTask
for each background task.
Task.create(() -> getResultsInBackground())
.onBefore(() -> doBeforeTaskOnMainThread())
.onAfter(() -> doAfterTaskOnMainThread())
.onSuccess(result -> {
// Do something with result.
})
.onError(ex -> {
// Log the exception.
})
.execute(getContext());
Task.create(() -> doSomethingInBackground())
.onBefore(() -> doBeforeTaskOnMainThread())
.onAfter(() -> doAfterTaskOnMainThread())
.onSuccess(() -> {
// Operation succeded. Do something.
})
.onError(ex -> {
// Log the exception.
})
.execute(this);