Forum Xamarin.iOS

How can I get the UI synchronization context for RX SubscribeOn?

I'm using Reactive Extensions Component in Xamarin for iOS app and I've been trying to use SubscribeOn to run the subscription callback on the UI thread, but it doesn't seem to be working.

source.SubscribeOn(SynchronizationContext.Current).Subscribe(res =>
{
  Console.WriteLine("Subscription thread {0}", Thread.CurrentThread.ManagedThreadId);
  BeginInvokeOnMainThread(() => Console.WriteLine("UI thread {0}", Thread.CurrentThread.ManagedThreadId));
}

In this example I get different thread IDs in both cases. I also tried to print out the thread ID before I do the Subscribe call and it is always the UI thread.

I thought that SynchronizationContext.Current will make the callback run on the UI thread, but it doesn't seem to have any effect. What am I doing wrong?

Posts

  • JakubArnold.8011JakubArnold.8011 CZMember ✭✭

    I've figured this out, it seems that I was supposed to use ObserveOn which changes the context of the subscription callbacks, instead of SubscribeOn, which changes the context of the subscription itself.

  • DamienSawyerDamienSawyer AUMember

    Thanks for that :-)

Sign In or Register to comment.