开发者

Why does TakeLast<T>() method not work on a ReplaySubject<T>

开发者 https://www.devze.com 2023-03-22 02:59 出处:网络
According the the MSDN documentation, the following code should output \'5\' to the console window.Instead, nothing is displayed.

According the the MSDN documentation, the following code should output '5' to the console window. Instead, nothing is displayed.

static void Main(string[] args)
{
     var o = new ReplaySubject<int>();

     o.OnNext(0);
     o.OnNext(1);
     o.OnNext(2);
     o.OnNext(3);
     o.OnNext(4);
     o.OnNext(开发者_高级运维5);

     o.TakeLast(1).Subscribe(Console.WriteLine);

     Console.WriteLine("Press any key to exit");
     Console.ReadKey();
}

Expected output:

5
Press any key to exit

Actual output:

Press any key to exit

Can anyone please explain why this is the case?


That's because you never notify the completion of the sequence, so TakeLast doesn't know the sequence is complete and continues to wait for the end of the sequence. This works as expected:

var o = new ReplaySubject<int>();

o.OnNext(0);
o.OnNext(1);
o.OnNext(2);
o.OnNext(3);
o.OnNext(4);
o.OnNext(5);
o.OnCompleted();

o.TakeLast(1).Subscribe(Console.WriteLine);
0

精彩评论

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