5-minute Observable.FromAsyncPattern sample for Windows Phone 7
Join the DZone community and get the full member experience.
Join For FreeIf you are looking to create code that models the observable.fromAsyncPattern, you can get started with some basic key types. You’ll need to be able to develop your own implementations from IObserver or IObservable.
Here you can develop the sequences you want with the data that you want. Learn the basic elements of observable sequences, how to get values into them, and how to select the values you want from them.
We can look to Windows Phone 7 as an example. As we have already noted, the Windows 7 mobile platform is galaxies ahead of Windows 6.5 and the market reports and brand love indicate a strong concurrence with that.
Not many developers know that Windows Phone 7 SDK comes pre-bundled with Reactive Extensions (Rx) that dramatically simplify work with asynchronous calls. Despite Windows mobile sales falling four percent last year, this is worth a look.
In this article, I am going to focus particularly on Observable.FromAsyncPattern that allows developers to combine two Begin/End calls. For example, when working on reading streams.
Developers can do this, by working into a single asynchronous call. If you are familiar with basic function composition, this won't be too hard of a topic to understand.
What is the Observable.FromAsyncPattern?
When you want to code Observable.FromAsyncPatterns it can be easy to feel overwhelmed. You’re putting the cart before the horse if you think this way. Like every coding project, you need to take this one at the one sequence at a time rate.
Microsoft’s coder describes his passion for the development process of this Windows experience as evangelism. It’s difficult not to get excited about it, but you need to take it one concept at a time.
You simply need to understand Rx coding and the basics of getting started. Observable sequences are the key here. It’s all about what values you are selecting.
You can use LINQ as well in order to understand your Rx coding. It’s about going back to the basics of functional programming. Each group of query operators will have its own function.
These will form dependencies of sorts wherein other operators can glean from them or be constructed from them. Deconstruct the code yourself and you will see how it works.
The deconstruction should show you some insight into the intricacies of the Rx staircase so to speak, with some queries and functional programming built-in as well. Additionally, you should be able to create custom fields and operators that meet your software’s needs.
To develop the sequence you are looking for, the Subscribe method as an Rx extension would be my first recommendation. Use the arrays of IObservables to determine what the Rx sequence would be, and the Rx libraries would indicate that as well.
To develop the sequence you are looking for, the Subscribe method as an Rx extension would be my first recommendation. Use the arrays of IObservables to determine what the Rx sequence would be, and the Rx libraries would indicate that as well.
The FromASyncPattern is a means of making calls. This coding will wrap one asynchronous call to either start or end an asynchronous method of making calls, actual phone calls. This is an effective tool for Windows Phone 7. A wide range of operators can take many different values and parameters here.
Understanding the ToAsync pattern will help with developing the FromAsyncPattern knowledge base but it isn’t necessary. You can use the Rx coding to develop your observable and assign the numbers to your generic functions. The coding for BeginInvoke and EndInvoke are ideal initial parameters.
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.
How Rx Theory Works
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.
Working With Specified Enumerations
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);
Using the Windows Phone 7 Example
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).
Create Your Sequence
Creating the observable sequence is the key here, and Windows Phone 7 has shown us it can be done. It’s an interesting concept and we know Windows brand passion is still there. You want to start with the sequence and then use it for consumption. When you do this, you have the wiggle room to use operators and develop the appropriate queries.
Opinions expressed by DZone contributors are their own.
Comments