开发者

IObservable - Ignoring the prior result

I have an IObservable which could be implemented as a BehaviorSubject, ReplaySubject or something similar.

In the following scenario I don't want the Subscriber to pick up the last cached value, instead I want it to pickup the next value to come through. Any ideas?

var subject = new BehaviorSubject(1);
subject.OnNext(2);

subject.Subscribe(x => Console.WriteLine(x));

subject.OnNext(3);

Note, the number I want printed is 3... I am guessing that I need to put one of the reactive extension methods between "subject.Subscribe" to开发者_运维技巧 get a new observable or something.


This should do what you want:

var subject = new BehaviorSubject(1);
subject.OnNext(2);

subject
       .Skip(1) // 
       .Subscribe(x => Console.WriteLine(x));

subject.OnNext(3);


In your specific snippet, you could just use a regular Subject and get what you want. Or for a generic cold Observable, you probably want someSubject.Publish().RefCount()


Does this do it for you?

var subject = new BehaviorSubject<int>(1);

subject.OnNext(2);

var co = subject.Publish();

co.Subscribe(x => Console.WriteLine(x));

subject.OnNext(3);

co.Connect();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜