Skip to content

check various task states before returning #1739

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 1, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
Expand Down Expand Up @@ -171,12 +172,9 @@ public static <T> T awaitEvenIfOnMainThread(Task<T> task)

task.continueWith(
TASK_CONTINUATION_EXECUTOR_SERVICE,
new Continuation<T, Object>() {
@Override
public Object then(@NonNull Task<T> task) throws Exception {
latch.countDown();
return null;
}
unusedTask -> {
latch.countDown();
return null;
});

if (Looper.getMainLooper() == Looper.myLooper()) {
Expand All @@ -185,8 +183,12 @@ public Object then(@NonNull Task<T> task) throws Exception {
latch.await();
}

if (task.isComplete()) {
if (task.isSuccessful()) {
return task.getResult();
} else if (task.isCanceled()) {
throw new CancellationException("Task is already canceled");
} else if (task.isComplete()) {
throw new IllegalStateException(task.getException());
} else {
throw new TimeoutException();
}
Expand Down