0

in these answers to other stackoverflow questions: https://stackoverflow.com/a/41360829/1932522 and https://stackoverflow.com/a/40557237/1932522 there are examples provided that show how to make use of a TaskCompletionSource to be able to run delayed calls within a task.

When implementing this in a simple example (provided below), I run into the problem that the line .continueWith( new TaskerB() )); will not compile, since the compiler expects a Task as first parameter: Incompatible equality constraint: Task<String> and String, I would expect this parameter should only be of the String type? Who can help in getting this code to work and explain me how to succesfully use make use of a TaskCompletionSource.

Note: the example is very simple, in real use I would for example run a Firestore action, set a listener and call the tcs.setResult(..) from inside the listener.

    public class StartTask implements Callable<Integer>{
        @Override
        public Integer call() throws Exception {
            return 1;
        }
    }

    public class TaskerA implements Continuation< Integer, Task<String>>{
        @Override
        public Task<String> then(@NonNull Task<Integer> task) throws Exception {
            final TaskCompletionSource<String> tcs = new TaskCompletionSource<>();
            tcs.setResult( "value: " + task.getResult() );
            return tcs.getTask();
        }
    }

    public class TaskerB implements Continuation< String, Void>{
        @Override
        public Void then(@NonNull Task<String> task) throws Exception {
            Log.d(TAG, "Output is: " + task.getResult());
            return null;
        };
    }

    public void runExample(){
        Tasks.call( new StartTask() )
            .continueWith( new TaskerA() )
            .continueWith( new TaskerB() ));
    }
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Peter
  • 10,910
  • 3
  • 35
  • 68

1 Answers1

2

Use continueWithTask instead:

Tasks.call(new StartTask())
        .continueWithTask(new TaskerA())
        .continueWith(new TaskerB());
SUPERCILEX
  • 3,929
  • 4
  • 32
  • 61