[Xamarin Blog] Why every mobile developer should be reactive

CraigDunnCraigDunn USXamarin Team Xamurai

Just posted: Why every mobile developer should be reactive on the Xamarin Blog.

Read the full article to learn about the Reactive Extensions for .NET library from Microsoft and how you can use it to create responsive mobile applications.

Use this thread to discuss or ask questions.


  • fpelliccionifpelliccioni ARMember
    edited June 2014

    Great article!

  • PapapizzariaPapapizzaria CAMember

    Almost every mobile application involves some sort of asynchronous operation – network requests, geolocation APIs, and even iOS’s Alert Views involve operations that call back your code. Using these asynchronous operations effectively are critical to creating great mobile experiences.
    Android Apps Developer

  • BoscoBosco USUniversity ✭✭

    @CraigDunn, I'm trying to use Reactive Extensions on a Xamarin.Forms project, and have run into an issue.

    I have a Page with a SearchBar, a ListView and a Map. The list contains a list of locations, which are plotted on the map. When a person types in the search bar, the list view is filtered - through bindings. Since XF.Maps doesn't support binding, I raise a SearchTextChanged event when the SearchText property on the SearchBar changes, and on that event regenerate the map. The problem here is that event can fire too fast and attempt to regenerate the map too frequently.

    I added Rx to throttle the SearchTextChanged event to only update the map if the SearchBar text hasn't changed in 1 second...

    // Create an Rx observable from the SearchTextChanged event
    IObservable<EventPattern<SearchTextChangedEventArgs>> searchTextChangedAsObservable = Observable.FromEventPattern<SearchTextChangedEventArgs>(ev => SearchTextChanged += ev, ev => SearchTextChanged -= ev);
    // Throttle the Rx observable output at 1sec intervals
    searchTextChangedAsObservable.Throttle(TimeSpan.FromSeconds(1)).Select(o => o.EventArgs.Text).Subscribe(FilterAndSearch);

    The problem here is that I get "you are calling a UIKit method that can only be invoked from the UI thread" error messages, because the Observable subscribe happens on a background thread, which is then trying to update the UI, which isn't allowed.

    I want to use ObserveOn(Scheduler.DispatchScheduler) and SubscribeOn(Scheduler.DispatchScheduler) to cause the subscribe to happen on the UI thread, but the DispatchScheduler isn't part of the Rx core - they're in Rx-XAML or Rx-WPF, neither of which can be added to my PCL project because they don't target the Android/iOS profile.

    Any thoughts?

  • CraigDunnCraigDunn USXamarin Team Xamurai

    Can you use Device.BeginInvokeOnMainThread to subscribe?

  • BoscoBosco USUniversity ✭✭

    @CraigDunn‌ Got it working! Thanks for your suggestion.

    I have to say, there's little or no documentation on that method, and it's not covered well in the Xamarin samples. After a lot of tinkering around wrapping the Rx functions, I found that I just had to wrap the following two lines with Device.BeginInvokeOnMainThread to make it work...

    Device.BeginInvokeOnMainThread(() => {
  • CraigDunnCraigDunn USXamarin Team Xamurai

    Cool, glad to hear.

    Yeah, the main thread switch is a tricky one. We don't bring it right up in the docs because it can cause uncertainty about when it's required; but then when you do need to know about it, it's hard to find/discover. It's mentioned in that one doc and is used in a few samples, but I hear you - could be clearer.

  • BrianFoodyBrianFoody AUMember

    Hi @Bosco . What Nuget package did you use for RX on Xamarin.Forms?

  • BrianFoodyBrianFoody AUMember

    I used RX-Main and all associated dependencies through NuGet which looks to be working fine so hopefully that's okay.

  • BoscoBosco USUniversity ✭✭

    Hey @BrianFoody

    Yes, I'm using the same NuGet package, Rx-Main, and all the satellite assemblies (Core, PlatformServices, Linq, etc).

    So far, it's working out fine for me.

  • BrianFoodyBrianFoody AUMember

    Thanks @Bosco . I had to add RX-Testing as well for proper unit tests that didn't slow down my app. This stack overflow has a good example for anyone interested;


  • Luis_ingsis27Luis_ingsis27 MXMember ✭✭

    Could you give me an example in xamarin forms please?

Sign In or Register to comment.