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?
-
Full example at http://stackoverflow.com/questions/37215071 – JP Ventura Nov 17 '16 at 00:36
2 Answers
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.

- 297,357
- 32
- 422
- 441
-
Thanks. I'm gonna test that later. Would you mind to talk about Tasks.forException() and Tasks.forResult()? Small examples would be nice too. – Fabricio Oct 20 '16 at 22:46
-
Those just create Tasks that are already resolved to the given result or exception. You probably will never use those, but are handy for testing when you need a Task with a known value. – Doug Stevenson Oct 20 '16 at 23:01
-
1BTW, nice blog about these tasks! Now I'm getting the point! You really should add other parts talking about the rest of the Task api. I'm in love with this. It fits so much well. I think I'm going to rewrite my api (I'm using retrofit) to support tasks. I hate when I have to chain calls one within the other, and RxJava IMO is so much complicated and ungly. Tasks are beautiful lol. Maybe that might be a good blog post for you too, how to chain retrofit calls using tasks. Just a suggestion ;) – Fabricio Oct 20 '16 at 23:04
-
-
Actually, I forgot to mention here that you can create Tasks from other sources of work (such as RxJava observables) using TaskCompletionSource. That's in part 4 of the blog. – Doug Stevenson Oct 21 '16 at 16:46
Suppose you have a Document
class, you could do as follow:
Create successfuly resolved task
Tasks.<Document>forResult(document);
Create a failed task
Tasks.forException(new RuntimeException("Cool message"));
Create from Callable
interface CreateDocument extends Callable<Document> {
@Override
Document call();
}
Tasks.call(new CreateDocument());
Create using task completion source
Task<Document> createDocument() {
TaskCompletionSource<Document> tcs = new TaskCompletionSource();
if (this.someThingGoesWrong()) {
tcs.setException(new RuntimeException("Cooler message"));
} else {
tcs.setResult(Document.factory());
}
tcs.getTask();
}

- 1,083
- 1
- 12
- 31

- 5,564
- 6
- 52
- 69