Hello again!
We’ll dive mainly into coding today.
This is part 2 of Reactive Extensions… in Xamarin Android???. Check that out if you’re interested in a little bit of history and use cases, or feel free to skip if you just want to dive into the bits and pieces of the code.
In reactive extensions, there is a timeline of events of what’s happening with something. We can take a real life example in which a person takes his/her wallet and dirty clothes, goes to the laundromat, washes the clothes, dries the clothes, puts them in the car, goes to the supermarket, buys food, and finally goes home with everything completed in the check list.
We’ll put this in the observer/observable pattern. The observable is the person in question. The observer is the follower of the story.
We’ll keep it simple by taking the example above with a shorter list of things to do: person exits his/her home, person goes to the supermarket, then person goes back home.
using System.IO; using System; using System.Reactive; using System.Reactive.Subjects; class Person { SubjectwhenFinished = new Subject (); IObservable WhenFinished => whenFinished; int step; void ExitHome() { // person exits home step = 1; whenFinished.OnNext(false); } void EnterSupermarket() { // person enters supermarket step = 2; whenFinished.OnNext(false); } void GoHome() { // done with tasks whenFinished.OnNext(true); } static void Main() { WhenFinished .Subscribe(finished => { string message; if (finished) { message = "finished"; } else { switch (step) { case 1: message = "entering supermarket"; EnterSupermarket(); break; case 2: message = "going home"; GoHome(); break; default: message = "doing something else"; break; } } Console.WriteLine(message); }); ExitHome(); } }
Let’s take a look at what’s happening here. We’ve made a subject that is a tracker to find the state of the person which is, in this case, the observer. Our observable lets us know if any updates are coming into the object, or rather, returning the subject. In our main method, we first initially subscribe to any events being pushed. No activity is being made at this time, that is until we call our ExitHome();
method.
Now as this method is being executed, it means we are updating our step to 1. Our observer then calls the OnNext(bool);
method, triggering our code to run within the WhenFinished.Subscribe(...)
method. The parameter finished
, in this case, would be false because we passed in false as a parameter in our call to OnNext(...);
. Now, since the step is 1, we call EnterSupermarket();
, which updates our step to 2. Similar happens with step 1, except now it jumps to step 2 calling GoHome();
in the process. Now because GoHome();
passes in true within the OnNext(...);
call, our subscription is done, at least for now…
So basically, anytime there is an observer and observable, with the observer calling OnNext(...);
, the observable will always subscribe to it.
This can be used in any Xamarin codebase as long as the libraries are referenced. Please let me know in the comments below if you’d like more examples of such.
Until next time!
Brian.