Why does Rx buffer continuously perform method when buffer contains no items?

后端 未结 2 1467
忘了有多久
忘了有多久 2021-01-05 01:59

I have a Rx Observable that acts as a buffer. Right now it performs the method in Subscribe either when it gets 10 items, or after 100 milliseconds, whichever comes first.

2条回答
  •  时光说笑
    2021-01-05 02:33

    Perhaps try it like this:

    public MyBufferClass(IMyServer server, IScheduler scheduler)
    {
        this.serverRequests = new Subject>>();
    
        this.serverRequests
            .GroupByUntil(x => 1, x => Observable.Timer(TimeSpan.FromMilliseconds(1000)))
            .SelectMany(x => x.ToArray())
            .Subscribe(buffer => GetMultipleItemsFromServer(buffer));
    }  
    

    That doesn't give you empty results.

    And the answer to your question regarding .Buffer(...) - that's the way it has been designed. Nothing more complicated than that.

提交回复
热议问题