The easiest way of creating a stream of observable is using Observable.Return (Make sure you get Rx-Main from Nuget packages before doing this. Also include using System.Reactive.Linq namespace).
What will this statement do?
Observable.Return(10);
This can be represented using the following 'marble diagram'
This will call OnNext(10) of the observer only once and then will call OnCompleted();
So Lets write this piece of code:-
Congratulations!! you have just written your first observable.
Now What do I do with it? Lets subscribe to it and use as I would use a stream of data:-
You should see something like this:-
Now lets see if OnCompleted was called. Lets add another piece of lambda as follows:-
myObseravle.Subscribe(
x => { Console.WriteLine("Next Value: " + x); },
() => { Console.WriteLine("Sequence Completed"); }
);
The new output:-
So we know that this behaves exactly as it was shown in the marble diagram.
What will this statement do?
Observable.Return(10);
This can be represented using the following 'marble diagram'
This will call OnNext(10) of the observer only once and then will call OnCompleted();
So Lets write this piece of code:-
var myObservable = Observable.Return(10);
Congratulations!! you have just written your first observable.
Now What do I do with it? Lets subscribe to it and use as I would use a stream of data:-
Console.WriteLine("Subscribing to my first observable");
myObseravle.Subscribe(
x => { Console.WriteLine("Next Value: "+ x); }
);
You should see something like this:-
Now lets see if OnCompleted was called. Lets add another piece of lambda as follows:-
myObseravle.Subscribe(
x => { Console.WriteLine("Next Value: " + x); },
() => { Console.WriteLine("Sequence Completed"); }
);
The new output:-
So we know that this behaves exactly as it was shown in the marble diagram.
No comments:
Post a Comment