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
I've figured this out, it seems that I was supposed to use
ObserveOn
which changes the context of the subscription callbacks, instead ofSubscribeOn
, which changes the context of the subscription itself.Thanks for that :-)