Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

An example of the AndroidScheduler subscribing an observable sequence on the UI Thread

View gist:4331287
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
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
Something went wrong with that request. Please try again.