Created
December 18, 2012 19:49
-
-
Save davemckeown/4331287 to your computer and use it in GitHub Desktop.
An example of the AndroidScheduler subscribing an observable sequence on the UI Thread
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace RxM4AExampleSolution | |
{ | |
using System; | |
using System.Reactive.Linq; | |
using System.Reactive.Mono.Android; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using Android.App; | |
using Android.OS; | |
using Android.Widget; | |
/// <summary> | |
/// Example implementation of RxM4A | |
/// </summary> | |
[Activity(Label = "RxM4AExampleSolution", MainLauncher = true, Icon = "@drawable/icon")] | |
public class Activity1 : Activity | |
{ | |
/// <summary> | |
/// The interval observable that is setup in a non ui thread | |
/// </summary> | |
private IObservable<long> obs1; | |
/// <summary> | |
/// The interval observable that is setup on the ui thread | |
/// </summary> | |
private IObservable<long> obs2; | |
/// <summary> | |
/// OnCreate Override | |
/// </summary> | |
/// <param name="bundle">The bundle</param> | |
protected override void OnCreate(Bundle bundle) | |
{ | |
base.OnCreate(bundle); | |
// Set our view from the "main" layout resource | |
this.SetContentView(Resource.Layout.Main); | |
FindViewById<TextView>(Resource.Id.activityThread).Text = Thread.CurrentThread.ManagedThreadId.ToString(); | |
this.obs2 = Observable.Interval(TimeSpan.FromSeconds(3)).ObserveOn(AndroidScheduler.UIContext(this)); | |
obs2.Subscribe(x => | |
{ | |
try | |
{ | |
FindViewById<TextView>(Resource.Id.handlerThread2).Text = Thread.CurrentThread.ManagedThreadId.ToString(); | |
FindViewById<TextView>(Resource.Id.interval2).Text = x.ToString(); | |
} | |
catch (Exception ex) | |
{ | |
var exs = ex.ToString(); | |
} | |
}); | |
Task.Factory.StartNew(this.SetupObservable); | |
} | |
/// <summary> | |
/// A delegate that setups an observable subscription from another thread | |
/// </summary> | |
private void SetupObservable() | |
{ | |
this.obs1 = Observable.Interval(TimeSpan.FromSeconds(3)).ObserveOn(AndroidScheduler.UIContext(this)); | |
// Get our button from the layout resource, | |
// and attach an event to it | |
Button button = FindViewById<Button>(Resource.Id.MyButton); | |
button.Click += (sender, args) => | |
{ | |
Toast.MakeText(this, "Starting Subscription", ToastLength.Long).Show(); | |
obs1.Subscribe(x => | |
{ | |
try | |
{ | |
FindViewById<TextView>(Resource.Id.handlerThread).Text = Thread.CurrentThread.ManagedThreadId.ToString(); | |
FindViewById<TextView>(Resource.Id.interval).Text = x.ToString(); | |
} | |
catch (Exception ex) | |
{ | |
var exs = ex.ToString(); | |
} | |
}); | |
}; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment