Google Play服务任务API:continueWith与continueWithTask [英] Google Play services Task API: continueWith vs continueWithTask

查看:470
本文介绍了Google Play服务任务API:continueWith与continueWithTask的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与任务有关.

task.continueWith()task.continueWithTask()有什么区别,您能为每个例子提供一个例子吗?

What's the difference between task.continueWith() and task.continueWithTask(), can you provide an example for each one?

推荐答案

The primary difference between continueWith and continueWithTask is one of the generic types of the Continuation you pass to it.

您可以将Continuation视为将某种输入类型转换为某种输出类型的东西.如果定义Continuation<IN, OUT>,其中IN是通过Task<IN>传递给then方法的输入类型,而OUT是该方法返回的类型.

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

当调用continueWith时,您传递了Continuation<IN, OUT>,并且预期then方法将计算并返回给定Task<IN>值作为输入的OUT值.如果您没有任何转换工作需要阻塞,则可以选择执行此操作,例如将整数数组减少为元素的总和或计算字符串中的单词数.

When calling continueWith, you pass a Continuation<IN, OUT>, and the then method is expected to compute and return the OUT value given a Task<IN> value as input. You might choose to do this if you don't have any blocking work to do for the conversion, such as reducing an integer array to the sum of its elements or counting the number of words in a String.

在调用continueWithTask时,您传递了Continuation<IN, Task<OUT>>,并且预期then方法将返回Task<OUT>,最终将生成OUT值(给定IN值作为输入).如果您能够将转换工作委派给现有的可重用任务,则可以选择此选项.

When calling continueWithTask, you pass a Continuation<IN, Task<OUT>>, and the then method is expected to return a Task<OUT> that eventually generates the OUT value, given the IN value as input. You might choose this if you are able to delegate the conversion work to an existing reusable Task.

实际上,您不需要选择一个或另一个来完成工作.这是首选样式的问题,或者您是否准备好委托Task而不是Continuation来委派对话.通常,仅当您有将转换链链接在一起的转换管道时,才使用Continuations.

Practically speaking, you aren't required to choose one or the other to do your work. It's a matter of preferred style, or if you have a nice Task ready to delegate your conversation rather than a Continuation. Typically you only use a Continuations if you have a pipeline of conversions to chain together.

此处的javadoc链接显示了Continuations的一些示例.另外,要了解更多信息,您可以在continueWithTask是Task API的唯一部分,我在该系列的任何地方都没有直接讨论,主要是因为从概念上讲,它与continueWith并没有太大区别.

The javadoc links here show some examples of Continuations. Also, to learn more, you can read about them in part three of my blog series. To be fair, continueWithTask is the only part of the Task API I don't directly discuss anywhere in that series, mostly because conceptually it doesn't differ very much from continueWith.

这篇关于Google Play服务任务API:continueWith与continueWithTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆