Google API for Google Play Services: continueWith vs continueWithTask

It's about Task .

What is the difference between task.continueWith() and task.continueWithTask() , can you provide an example for each of them?

+7
android firebase
source share
3 answers

The main difference between continueWith and continueWithTask is one of the common Continuation types that you pass to it.

You can think of a continuation as something that converts some type of input into some type of output. If you define Continuation<IN, OUT> , where IN is the input type passed to its method then using Task<IN> , and OUT is the type returned by the method.

When you call continueWith you pass Continuation<IN, OUT> , and it is expected that the then method will evaluate and return the OUT value specified as the value of Task<IN> . You can do this if you do not have any blocking work for conversion, for example, to reduce an integer array to the sum of its elements or count the number of words in a string.

When you call continueWithTask you pass Continuation<IN, Task<OUT>> , and the then method is expected to return Task<OUT> , which ultimately generates an OUT value, given the IN value as the input. You can choose this if you can delegate the conversion job to an existing reusable task.

In fact, you are not required to choose one or the other to do your job. This is a matter of preferred style, or if you have a good Task ready to delegate your conversation, not Continuation . Usually you only use Continuations if you have a conversion pipeline to merge.

The javadoc links provide examples of continuations. In addition, to learn more, you can read about them in part three of my blog series. In fairness, we note that continueWithTask is the only part of the Task API that I don’t discuss in this series, mainly because conceptually it is not much different from continueWith .

+7
source share

Just to add to what Doug said, I would say the following:

continueWith completes the result of the then method in Task . continueWithTask will not; he expects the then method to return Task and thus avoid double-packing the task inside the task.

continueWithTask ideal if you want to use Continuation and TaskCompletionSource together.

+4
source share

I would like to add that continueWith and continueWithTask really caused me problems, obviously because I really did not understand the API, but also the naming confused me. Perhaps an example of my refusal may prevent others from doing the same.

TL; DR

When to use which method:

  • Use continueWith if you want to use the result of the previous task and return the new result to the Continuation then method. And you need to pass it on to another sequel or use it later in the listeners. Return value continueWith is a task that simply WRAPS returns the return value of the then method.

  • Use continueWithTask if you want to use the result of the previous task, and use it a little in the new task that you create in your Continuation then method. The return value of continueWithTask is the task that you create inside then and has its own common result.


DO NOT return the task in its continuation and use it with continueWith. It can compile and run for years without warning, but also without doing this work.

IF you directly add a listener after continueWith this listener will give you the task TO REVERSE the result of your return then cost. If this return value is the task itself, do not expect it to be executed (!!!).


Long story!

I had a chain of calls like:

 private Task<SomeResult> getTask() { PreloadingTask.create().continueWith(additionalTask()).addOnCompleteListener(task -> { if (task.isSuccessful()) { source.setResult(someResult); } else { source.setException(new Exception()); } }); return source.getTask(); } 

So, as you can see, additionalTask() should return some type of Continuation<IN, OUT> , which implements the method, and then

 @Override public OUT then(Task<IN> task){ ... }}; 

In my case, I did not need to check OUT , because I just wanted to do additional calculations AFTER the PreloadingTask and redirects were completed, this would lead to the continuation of additionalTask() .

I wanted to execute a task from additionalTask() , and then onCompleteListener should be called.

 private Continuation<PreviousResult, Task<Void>> additionalTask() { return task -> { PreviousResult r = task.getResult(); simpleResultModification(r); return new AdditionalTask(r); ); }; } 

What happened? onCompleteListener was called directly because my then method was executed and returned its result, which was an instance of AdditionalTask . Then this AdditionalTask wrapped in another task and passed the result to onCompleteListener.

And my ExtraText was not executed.

What you should use continueWithTask if you return the task during this time.

+1
source share

All Articles