DZone
Mobile Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Mobile Zone > 5 minute Observable.FromAsyncPattern sample for Windows Phone 7

5 minute Observable.FromAsyncPattern sample for Windows Phone 7

Denzel D. user avatar by
Denzel D.
·
Nov. 11, 10 · Mobile Zone · Interview
Like (0)
Save
Tweet
16.85K Views

Join the DZone community and get the full member experience.

Join For Free

Not lots of developers know that Windows Phone 7 SDK comes pre-bundled with Reactive Extensions (Rx) that dramatically simplify work with asynchronous calls. In this article, I am going to focus particularly on Observable.FromAsyncPattern that allows developers combine two Begin/End calls (for example, when working on reading streams) into a single asynchronous call, and if you are familiar with basic function composition, this won't be too hard of a topic to understand.

For now, rather than talk about the theory behind FromAsyncPattern, let's take a look at a specific example - handling a web request. The standard way of doing this would be initializing a HttpWebRequest and asynchronously getting the response via BeginGetResponse (also passing a callback that will handle it).

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://bing.com");
request.Method = "GET";
request.BeginGetResponse(new AsyncCallback(GetResponse), request);

So the callback can handle the response the way you want:

void GetResponse(IAsyncResult res)
{
HttpWebRequest request = (HttpWebRequest)res.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(res);
Debug.WriteLine(response.ContentType);
}

Rx allows you to go around the whole async call in one line. So to say:

var n = Observable.FromAsyncPattern<WebResponse>(request.BeginGetResponse, request.EndGetResponse)();

If you analyze n, you will see that its type is defined as IObservable<WebResponse>. In its core, IObservable might be a new interface to some of the developers that are currently reading this. So let's dive into some theory now.

Usually, when you work with specific enumerations, there is IEnumerable<T>/IEnumerator<T> that can help you navigate through a generic collection of returned objects. GetEnumerator (caled out of IEnumerable) will get the associated IEnumerator, that will go through each object in the collection (via MoveNext) setting the Current value to the selected object. A pretty simple concept that shows how data can be pulled from a collection.

IObservable on the other side is the complete opposite of IEnumerable because it reverses the process - instead of data being retrieved by the program via IEnumerator, it is now pushed back to it by the environment via IObserver, with which the application interacts (talking about the concept of interface duality here).

When you need to obtain the data that is being pushed as a result of the call (in this case, in n), you need to use Subscribe and pass a handling method:

n.Subscribe(HandleResult);

What's done behind the scene here is that an observer is actually created that will be handling the pushed data. It might be a bit confusing as to why the returned type is IDisposable and the answer is simple - so that it will be possible to un-subscribe and dispose the observer once there is no more need to receive pushed data.

And then again, you can handle the result as you want:

void HandleResult(WebResponse res)
{
Debug.WriteLine(res.ContentType);
}

Of course, this entire operation can be unified in one line:

Observable.FromAsyncPattern<WebResponse>(request.BeginGetResponse, request.EndGetResponse)().Subscribe(HandleResult);

One thing to remember here is that on Windows Phone 7, the maximum number of passed parameters is 2 (Func<T1,T2,TResult>) - in a regular application this extends to 29 overloads and 14 possible parameters. Working directly with System.Linq.Observable is currently not possible on the phone and will fail to compile, the issue being "Error 427 'FromAsyncPattern' is not supported by the language".

On the device itself, many asynchronous tasks can be simplified by using FromAsyncPattern, since you can combine various asynchronous functions - even the least common ones, since there are some overloads present that allow you to pass additional parameters that will be passed to the asynchronous functions. One important consideration to remember is that there should be the starting and end methods (to start the async processing and to end it accordingly). 

Windows Phone

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Testing Schema Registry: Spring Boot and Apache Kafka With JSON Schema
  • Creating Event-Based Architecture on Top of Existing API Ecosystem
  • What Is HttpSession in Servlets?
  • A Developer Evangelist's Thoughts on Angular 2

Comments

Mobile Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo