TL;DR
Just like Parse did with Bolts, Google also provided a task framework that implement JavaScript promises. So, instead of nesting listeners, you can create a sequence of tasks.
The result will be sent to addOnSuccessListener
if all tasks execute successfully.
If any of them fail during the use case execution, the sequence will be aborted and the exception is passed to addOnFailureListener
.
public Task<Course> execute() {
return Tasks.<Void>forResult(null)
.then(new GetUser())
.then(new GetCourse());
}
public void updateInBackground() {
Tasks.<Void>forResult(null)
.then(new GetUser())
.then(new GetCourse())
.addOnSuccessListener(this)
.addOnFailureListener(this);
}
@Override
public void onFailure(@NonNull Exception error) {
Log.e(TAG, error.getMessage());
}
@Override
public void onSuccess(Customer customer) {
// Do something with the result
}
Description
Suppose you wish to download two object of type User
and Course
from Firebase.
You need to create the first task of your sequence using the Tasks API. Your options are:
I prefer the first option mostly due code legibility. If you you need run the tasks in your own executor, you should use the first or second option.
Now let's create two Continuation
tasks two download each one:
class GetUser implements Continuation<Void, Task<User>> {
@Override
public Task<User> then(Task<Void> task) {
final TaskCompletionSource<User> tcs = new TaskCompletionSource();
ref1.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onCancelled(DatabaseError error) {
tcs.setException(error.toException());
}
@Override
public void onDataChange(DataSnapshot snapshot) {
tcs.setResult(snapshot.getValue(User.class));
}
});
return tcs.getTask();
}
}
and
class GetCourse implements Continuation<User, Task<Course>> {
@Override
public Task<Course> then(Task<User> task) {
final User result = task.getResult();
final TaskCompletionSource<Course> tcs = new TaskCompletionSource();
ref2.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onCancelled(DatabaseError error) {
tcs.setException(error.toException());
}
@Override
public void onDataChange(DataSnapshot snapshot) {
tcs.setResult(snapshot.getValue(Course.class));
}
});
return tcs.getTask();
}
}
According the documentation, call getResult()
and allow the RuntimeExecutionException to propagate to propagate failure from the completed Task.
The RuntimeExecutionException
will be unwrapped such that the Task returned by continueWith(Continuation)
or continueWithTask(Continuation)
fails with the original exception.