Use Scan:
observable
.Scan(0, (count, _) => count + 1)
.Subscribe(count => Console.WriteLine("Current count is " + count));
Question
I have the following Observable.
IObservable<MyDto> observable;
Now in order to count the number of items in Observable I cannot use the following code because my observable is Hot and long running and subscription to the 'count' will never be called.
var count = observable.Count()
I am looking to get the count every time an item arrives, here is something I am trying to do
observable.Subscribe(o => Console.WriteLine(" Object received "));
observable.Count().Subscribe(c => Console.WriteLine("Current count is " + c.ToString() + " but this is not final count, more are coming"));
How can I achieve this?
Solution
Use Scan:
observable
.Scan(0, (count, _) => count + 1)
.Subscribe(count => Console.WriteLine("Current count is " + count));