In such situations, an elegant solution would be to use the Where statement right after the buffer to filter out any empty results. Something like that:
stream .Buffer (...) .Where (x => x.Any()) .Subscribe (x => {...}, ex => {...});
As to why Buffer acts this way, I think it's better to place an empty collection and let the consumer choose what to do with it than to internalize it and deny this opportunity.
In a separate note, I would not have had your server call inside the subscription block. I think itβs best to have any asynchronous operations as part of the composition of the Rx stream itself and limit the βSubscribeβ action to any light operations that relate to the end result, that is, updating the user interface, successful execution / failure of logging, etc. . Something like:
(from request in serverRequests .Buffer (TimeSpan.FromMinutes (1)) .Where (x => x.Any()) from response in Observable.Start(server.GetMultipleItems(...)) select response) .Subscribe (x => {}, ex => {});
Benefits of this include:
-You can use further Rx-operators when calling the server, such as Timeout (), Retry (), Catch (), etc.
- Ability to handle any pipeline errors in overload Subscribe ()
Independent pipeline scheduling and Subscription action using SubscribeOn () / ObserveOn ().
Chris
source share