This library does the same thing AsyncTask does – runs code on background thread and gets result back to the main thread. But it is simpler, cleaner, easier to understand, straightforward to use, reqires no arcane knowledge to be used. And it requires less boilerplate code than RxJava.
This is how task execution looks in the code:
Task task = new Task();
Tasks.execute(task, callback);
Tasks are implemented as simple Callables
:
private static class MyCallable implements Callable<String> {
public String call() throws Exception {
// Do you networking (i.e.) here
}
}
You will get results in your callback object (in the main thread):
private final Callback<String> callback = new Callback<String>() {
public void onFinish(String result, Callable callable, Throwable e) {
// Here you'll get either throwable or result
if (e == null) {
// do something with the result
} else {
// handle error
}
}
};
repositories {
jcenter()
}
dependencies {
compile 'rongi.async-task:async-task:1.0.1'
}
- Task code and result handlers are separated. This means that it is possible to use same async-task implementation in diferent situations. This is not an option with vanila Android AsyncTask.
- Callbacks are weakly referenced so stalled task will not leak your activity.
- This also means that you don't have to unsubscribe from task in onStop().
- Callbacks can be forced to be strong referenced via options.
- You can "abandon" task. Abandoned tasks do not call it's callback when done. Do it by storing
Handle
object returned fromTasks.execute(callable, taskCallback)
and then callmyHandle.abandon()
- It is possible to run tasks with you own
ThreadPoolExecutor
just specify one inOptions