0

I've got following code:

Observable.Interval(TimeSpan.FromMilliseconds(2500)).SubscribeOn(XXX).ObserveOn(YYY).Subscribe( t => SendCounter(t), e => HandleException(e));

Where XXX, YYY are Schedulers.

Inside SendCounter(t) I set a text with t value.

The problem is that when I run the code I got this error:

'only the original thread that created a view hierarchy can touch its views'

I'm using this component: https://components.xamarin.com/view/rxforxamarin

jzeferino
  • 7,700
  • 1
  • 39
  • 59
Marcin Bortel
  • 1,190
  • 2
  • 14
  • 28
  • 1
    Possible duplicate of [error: Only the original thread that created a view hierarchy can touch its views in xamarin](http://stackoverflow.com/questions/21162096/error-only-the-original-thread-that-created-a-view-hierarchy-can-touch-its-view) – SushiHangover Jul 02 '16 at 12:17
  • not too sure how to Use ObserveOn() and SubcribeOn() myself in xamarin. tried Scheduler.NewThread but thats deprecated? – filthy_wizard Oct 18 '16 at 13:59

3 Answers3

2
var uiThread = SynchronizationContext.Current;

Observable
    .Interval(TimeSpan.FromMilliseconds(2500))
    .SubscribeOn(XXX)
    .ObserveOn(uiThread)
    .Subscribe( t => SendCounter(t), e => HandleException(e));
xleon
  • 6,201
  • 3
  • 36
  • 52
1

Make sure that you Observe on the main thread so that you can affect the view directly.

0

If you're using ReactiveUI use your code like this for e.g:

searchButton
.SelectMany(async x => await executeSearch(x))
.ObserveOn(RxApp.MainThreadScheduler)
.ToProperty(this, x => x.SearchResults, out searchResults);

ReactiveUI handbook

Pellet
  • 2,254
  • 1
  • 28
  • 20