Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.
Observable vs. Task
This article explains when to use observables and when to use tasks.
In Implementing GeoCordinateWatcher as a Reactive Service we saw how to convert an event-based asynchronous API into an API based on observables, while in Bringing async/await to the Contacts service we saw how to convert to an API based on tasks.
This article answers the question "in what circumstances should each of the techniques be used".
When to use observables
Reactive Programming allows you to compose asynchronous and event-based programs using observable sequences and LINQ-style query operators. It is very suited to handling, combining and filtering streams of events.
The need to combine several event streams or filtering is a good clue for the use of observables.
When to use tasks
As the name suggests, a task is something that has a beginning and an end. So, all asynchronous APIs based on callbacks or events are candidates to be converted into a task-based asynchronous API.
Another way to look at this is:
- Tasks are for a single future value
- Observables are for multiple future values
The following table shows the correspondence between asynchronous APIs and their synchronous counterparts:
|Single Value|| -
// synchronous call
// asynchronous call
|Multiple Values|| Enumerable
// Shopping database
// Stock trade events