如何使用Google Play服务任务API为Firebase创建自定义任务 [英] How to create custom tasks for Firebase using the Google Play services Task API

查看:124
本文介绍了如何使用Google Play服务任务API为Firebase创建自定义任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建自定义任务

a>像 firebase中的这些内容为了链接我的API异步调用。我怎么能做到这一点?

解决方案

有几种方法可以使用Play服务任务API创建自定义任务。 p>

首先,有 Tasks.call(Callable)。您传递的Callable计划在主线程上立即执行,并且返回一个Task,并带有Callable返回类型的泛型参数。此任务使用该返回值成功解析,如果Callable引发异常,则返回错误。



另一种方法是 Tasks.call(Executor,Callable),这与其他方法完全一样,除了给定的可调用在一个由给定的执行程序。这取决于您是否找到或创建适合您工作的执行程序。



最后,还有 TaskCompletionSource ,它可以让你创建一个任务并手动将其解析为其他项目的结果成功或失败的工作与任务没有直接关系。

有关更多详细信息,请查看我的关于任务的博客系列。我在第三部分中介绍了这些方法和第四部分


I'd like to create custom tasks like these ones in firebase in order to chain my API async calls. How can I achieve that?

解决方案

There are a few ways to create a custom task using the Play services Task API.

First, there is Tasks.call(Callable). The Callable you pass is scheduled for immediate execution on the main thread, and you get a Task in return, with a generic parameter of the return type of the Callable. This Task resolves successfully with that return value, or an error if the Callable throws an exception.

The other method is Tasks.call(Executor, Callable), which is exactly like the other method, except the given callable is scheduled for immediate execution on a thread managed by the given Executor. It's up to you to find or create an Executor that's appropriate for your work.

Lastly, there is also TaskCompletionSource, which lets you create a Task and manually resolve it to success or failure from the result of some other item of work not directly related to a Task.

For more details, check out my blog series on Tasks. I cover these methods in part three and part four.

这篇关于如何使用Google Play服务任务API为Firebase创建自定义任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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