The power of Reactive Programming and CancellationTokens
Tags: mobileapps, csharpprogramming, xamarinforms, xamarin
Published: 2020-03-30 19:29:55

---
title: The power of Reactive Programming and CancellationTokens
published: true
date: 2020-02-09 22:54:19 UTC
tags: mobile-apps,c-sharp-programming,xamarin-forms,xamarin
canonical_url: 
---

One of the most common scenarios that a mobile developer could encounter is to have an async method that depends on a platform capability like Internet connectivity, Bluetooth, Camera,…

While checking for the capability availability sound straight forward and a method like this one can do the job

```
public async Task<string> GetDataAsync()

{

if (Connectivity.NetworkAccess != NetworkAccess.Internet

||Connectivity.ConnectionProfiles.All(x=>x!=ConnectionProfile.WiFi))

return null;

HttpResponseMessage response;

using (var httpClient = new HttpClient())

{

response = await httpClient.GetAsync("https://medium.com/feed/@Medium");

}

response.EnsureSuccessStatusCode();

return await response.Content.ReadAsStringAsync();

}
```

What if after the checking pass or while downloading the internet connection gets interrupted, In this case, you could get an HttpException may be in this case it can handle the exception but what the connection type changed from wifi to cellular for example, now you will download the data from the user cellular data.

The good news is that HttpClient.GetAsync has an overload that takes a CancellationToken as a parameter, so now all that we need is to use this overlead and pass to it a CancellationToken that gets canceled when the internet connection changed.

but here one challenged the xamarin essential expose a ConnectivityChanged event and implementing a CancellationTokenSource based on events is a bit tricky and can introduce more bugs to your code.

In this article, I will provide you with a reusable method that you can use to convert any .net event to a CancellationTokenSource.

![](https://cdn-images-1.medium.com/max/1024/1*NdO1w22wkOOXkcf9H5IKQA.png)

We first will convert our Event to an [Observable](https://docs.microsoft.com/en-us/previous-versions/dotnet/reactive-extensions/hh244252(v%3Dvs.103))using C# reactive extension

```
Observable

.FromEventPattern<Xamarin.Essentials.ConnectivityChangedEventArgs>(

handler => Xamarin.Essentials.Connectivity.ConnectivityChanged += handler, handler => Xamarin.Essentials.Connectivity.ConnectivityChanged -= handler)

.FirstAsync(x=>x.EventArgs.NetworkAccess!=NetworkAccess.Internet)
```

and then will convert the [observable](https://docs.microsoft.com/en-us/previous-versions/dotnet/reactive-extensions/hh244252(v%3Dvs.103))to a custom implementation of CancellationTokenSource

{% gist https://gist.github.com/TheFo2sh/b9e15108e399a0c88fbc2294a7ddd6c9 %}

our ObservableCancellationTokenSource is just an adapter that takes IObservavle As a parameter subscribe on it and just call the cancellation method when the observable publish a new value.

The dispose method takes care of disposing of the [CanellationTokenSource](https://docs.microsoft.com/en-us/dotnet/api/system.threading.cancellationtokensource?view=netframework-4.8)and the subscription so no memory leaks will get introduced.

now the modified version of our GetDataAsync method should look like that

```
public static async Task<string> GetDataAsync(CancellationToken cancellationToken)

{

if (cancellationToken.IsCancellationRequested)

return null;

HttpResponseMessage response;

using (var httpClient = new HttpClient())

{

response = await httpClient.GetAsync("https://medium.com/feed/@Medium", cancellationToken);

}

response.EnsureSuccessStatusCode();

return await response.Content.ReadAsStringAsync();

}
```

and the caller will look like

```
var cancellationTokenSource = Observable

.FromEventPattern<ConnectivityChangedEventArgs>(

handler => Connectivity.ConnectivityChanged += handler,

handler => Connectivity.ConnectivityChanged -= handler)

.FirstAsync(x => x.EventArgs.NetworkAccess != NetworkAccess.Internet

||Connectivity.ConnectionProfiles.All(connectionProfile => connectionProfile != ConnectionProfile.WiFi))

.ToCancellationTokenSource();

using (cancellationTokenSource)

{

var result = await FullState.GetDataAsync(cancellationTokenSource.Token);

}
```

### Rx Bonus (combine events)

as we are using RX observable, we have the opportunity of combining multiple events to a single CancellationToken.

```
Func<EventPattern<ConnectivityChangedEventArgs>, bool> connectivityPredicate = x => x.EventArgs.NetworkAccess != NetworkAccess.Internet

||Connectivity.ConnectionProfiles.All(connectionProfile => connectionProfile != ConnectionProfile.WiFi);

Func<EventPattern<EnergySaverStatusChangedEventArgs>, bool> batteryPredicate = x => x.EventArgs.EnergySaverStatus == EnergySaverStatus.On;

var connectivityObservable = Observable

.FromEventPattern<ConnectivityChangedEventArgs>(

handler => Connectivity.ConnectivityChanged += handler,

handler => Connectivity.ConnectivityChanged -= handler);

var batteryObservable = Observable

.FromEventPattern<EnergySaverStatusChangedEventArgs>(

handler => Battery.EnergySaverStatusChanged += handler,

handler => Battery.EnergySaverStatusChanged -= handler);

var cancellationTokenSource = connectivityObservable

.CombineLatest(batteryObservable,(connectivityArgs, batteryArgs) => (connectivityArgs, batteryArgs))

.FirstAsync(x=>connectivityPredicate(x.connectivityArgs)|| batteryPredicate(x.batteryArgs))

.ToCancellationTokenSource();
```

similar behavior can be achieved using CancellationTokenSource.CreateLinkedTokenSource but I prefer the observable approach.