androidbolts-framework

In Bolts, how do you use continueWith() vs continueWithTask()?


Besides sync vs async, the differences in their documentation is confusing to me. The examples on their github page still look like the continuations are being called synchronously.

continueWith() Adds a synchronous continuation to this task, returning a new task that completes after the continuation has finished running.

continueWithTask() Adds an asynchronous continuation to this task, returning a new task that completes after the task returned by the continuation has completed.


Solution

  • When you have helper methods that return a Task object, you cannot use continueWith() or onSuccess() because the Bolts code won't treat it as a Task and wait for its execution. It would treat the Task as a simple data result.

    Basically, this won't work, because the resulting task of this chain is a Task<Task<Void>>:

    update().onSuccess(new Continuation<ParseObject, Task<Void>>()
    {
      public Task<Void> then(Task<ParseObject> task) throws Exception
      {
        return Task.delay(3000);
      }
    }) // this end returns a Task<Task<Void>>
    

    But this will work and the chain will return a Task<Void>:

    update().onSuccessTask(new Continuation<ParseObject, Task<Void>>()
    {
      public Task<Void> then(Task<ParseObject> task) throws Exception
      {
        return Task.delay(3000);
      }
    }) // this end returns a Task<Void>