MOBILE

Windows Phone 7 Development : Handling Data Connection Issues with Rx.NET

4/23/2011 3:57:59 PM
On a phone, slow or lost data connections are a fact of everyday life. Ideally, phone applications should detect such connections and provide a recovery mechanism to deal with them. Two potential ways to deal with slow or lost connectivity on the phone are: (1) let the user decide whether the application should retry what it was doing before the connection timed out or lost, and (2) provide an automated retry mechanism.

Rx.NET can aid in both of those scenarios. Furthermore, Rx.NET includes a special Timeout operation that will generate a timeout error if it does not receive data, such as a web service callback, from its Observable within a user-specified interval. Let's take a look at the Timeout operation in action. Let's change the WireUpWeatherEvents function to time out if it does not get any data for two seconds:

  1. Replace the WireUpEvents() function of the WeatherRx application with the following code:

    private void WireUpWeatherEvents()
    {
    var weather = GetWeatherSubject();
    weather.ObserveOn(Deployment.Current.Dispatcher)
    .Timeout(TimeSpan.FromSeconds(2))
    .Subscribe(evt =>
    {
    if (evt.EventArgs.Result.Details != null)
    {
    lblWeatherFahrenheit.Text = "Current Weather, Fahrenheit: " + evt.EventArgs.Result.Details[0].MinTemperatureF.ToString() + " - " + evt.EventArgs.Result.Details[0].MaxTemperatureF.ToString();
    lblCelsius.Text = "Current Weather, Celsius: " + evt.EventArgs.Result.Details[0].MinTemperatureC.ToString() + " - " + evt.EventArgs.Result.Details[0].MaxTemperatureC.ToString();
    imgWeather.Source = new BitmapImage(new Uri(evt.EventArgs.Result.Details[0].WeatherImage, UriKind.Absolute));
    }
    },



    ex => {
    Deployment.Current.Dispatcher.BeginInvoke(() => lblStatus.Text = ex.Message);
    }
    );
    }


Now run the application and notice how after two seconds, it immediately times out and displays the timeout exception text on the emulator. What happened? You did not even get a chance to specify the zip code to show the weather for.

Our code needs a little refactoring, or changing around. In the code so far, you subscribed to the web service's events immediately on application launch, and since you did not get any data two seconds after the launch of the application, that subscription timed out. The change that you need to make is to subscribe to the web service's events right before you invoke that web service, yet you have to be careful to create this subscription just once.

  1. Remove the call to WireUpWeatherEvents from the MainPage constructor and place it within the WireUpKeyEvents function to make that function look like the following:

    private void WireUpKeyEvents()
    {
    var keys = Observable.FromEvent<KeyEventArgs>(txtZipCode, "KeyUp").Throttle(TimeSpan.FromSeconds(1)).DistinctUntilChanged();
    keys.ObserveOn(Deployment.Current.Dispatcher).Subscribe(evt =>
    {
    if (txtZipCode.Text.Length >= 5)
    {
    WireUpWeatherEvents();
    weatherClient.GetWeatherByZipCodeAsync(txtZipCode.Text);
    }
    });

    }


Now the timeout feature should work properly. Notice, however, that it will most likely take slightly more than two seconds to return a valid response from the Weather service.

Rx.NET also provides a Retry method that optionally takes a parameter for the number of times to retry to re-subscribe to the Observable collection. If you don't specify that parameter, Rx.NET will try to re-subscribe to the Observable collection indefinitely. One way to deal with an absent or slow connection is to retry the subscription two or three times, and then, if unsuccessful, give the user the option to either retry once more or cancel.

Other  
  •  Windows Phone 7 Development : Handling Errors in Rx.NET
  •  Windows Phone 7 Development : Using Rx.NET with Web Services to Asynchronously Retrieve Weather Data
  •  Windows Phone 7 Development : Media - Adding Sounds to an Application
  •  iPhone Application Development : Building a Multi-View Tab Bar Application (part 4) - Implementing the Summary View
  •  iPhone Application Development : Building a Multi-View Tab Bar Application (part 3) - Implementing the Volume View
  •  iPhone Application Development : Building a Multi-View Tab Bar Application (part 2) - Implementing the Area View
  •  iPhone Application Development : Building a Multi-View Tab Bar Application (part 1)
  •  Windows Phone 7 Development : Working with Video (part 2) - Coding the Application
  •  Windows Phone 7 Development : Working with Video (part 1)
  •  Windows Phone 7 Development : Plotting an Address on a Bing Maps Map and Working with the Bing Maps Service
  •  Windows Phone 7 Development : Using GeoCoordinateWatcher and the Bing Maps Control to Track Your Movements
  •  iPhone Application Development : Creating a Multi-View Toolbar Application (part 3) - Adding Toolbar Controls
  •  iPhone Application Development : Creating a Multi-View Toolbar Application (part 2) - Instantiating the View Controllers
  •  iPhone Application Development : Creating a Multi-View Toolbar Application (part 1)
  •  Windows Phone 7 Development : Using Location Services - Simulating the Location Service
  •  Introducing the Windows Phone Location Service and Mapping APIs
  •  iPhone Application Development : Implementing a Custom Picker View (part 4) - Tweaking the Picker UI
  •  iPhone Application Development : Implementing a Custom Picker View (part 3) - Reacting to a Picker View Choice
  •  iPhone Application Development : Implementing a Custom Picker View (part 2)
  •  iPhone Application Development : Implementing a Custom Picker View (part 1)
  •  
    Top 10
    Ultrabook Supertest (Part 2) - Acer Aspire Timeline U M5
    Ultrabook Supertest (Part 1) - Acer Aspire Timeline U M3
    Ultrabook Supertest (Part 8)
    Ultrabook Supertest (Part 7) - Lenovo U310
    Ultrabook Supertest (Part 6) - HP Envy 6
    Ultrabook Supertest (Part 5) - HP Envy 4
    Ultrabook Supertest (Part 4) - Dell Inspiron 14z
    Ultrabook Supertest (Part 3) - Asus Zenbook Prime UX31A
    How To Make The Most Of Dropbox (Part 2)
    How To Make The Most Of Dropbox (Part 1)
    Most View
    Remote Administration of Exchange Server 2010 Servers : RDP with Exchange Server 2010 (part 2)
    Kingston HyperX T1 Performance RAM – Hyper-Formance Memory
    Expert computing advice (Part 3) - Using heritage format files & Free Screen Capture Software
    Server-Side Browser Detection and Content Delivery : Mobile Detection (part 3) - Transcoders
    Jexaa Jex Tab Evo: Slow and Bad
    Programming the Mobile Web : Ajax Support
    SQL Server 2008 : Explaining Advanced Query Techniques - Controlling Execution Plans (part 2)
    iCar - Imagine The Possibilities
    How to Protect Your Mobile Devices
    LCD NEC EX201W
    Hot Five Digital Devices - August 2012
    Laptop For All Budgets (Part 1)
    Birds Of Prey (Part 3) - Behind the scenes
    .NET Security : Programming the Event Log Service (part 1) - Querying the Event Log System, Using Event Sources
    Corsair Vengeance 1300 Analog Gaming Headset
    Design and Deploy High Availability for Exchange 2007 : Design Hub Transport High Availability
    MySQL Server Monitoring (part 1) - SQL Commands
    Fujifilm FinePix F800EXR Super-Zoom Compact With WiFi Connection
    Deploying the Client for Microsoft Exchange Server 2010 : Preparing the Deployment
    Optimizing for Vertical Search : Optimizing for Product Search