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

≯℡__Kan透↙ 提交于 2019-12-09 17:41:04

问题


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.


回答1:


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>


来源:https://stackoverflow.com/questions/31735318/in-bolts-how-do-you-use-continuewith-vs-continuewithtask

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!