有没有办法强制 Observable.Buffer 在缓冲时间结束之前刷新?
在示例中:
mSubscription = mFluxObservable.Buffer(new TimeSpan(0, 0, 1, 30)).Subscribe(o => saver(o, iSessionId));
我想在 1:30 周期结束之前刷新数据!
这对我有用:
var subject = new Subject<Unit>();
var closing = Observable
.Timer(new TimeSpan(0, 0, 1, 30))
.Select(x => Unit.Default);
var query =
mFluxObservable
.Buffer(() => Observable
.Amb(subject, closing)
.Take(1));
现在我只需要调用subject.OnNext(Unit.Default)
来强制缓冲区刷新。刷新后立即启动新的缓冲区。
...并且使用窗口的原理基本相同:
var bufferPeriod = TimeSpan.FromSeconds(1.5);
var source = Observable.Interval(TimeSpan.FromMilliseconds(100)).Take(50);
//source.Buffer(bufferPeriod).Dump();
var bufferFlush = new Subject<long>();//Or Subject<Unit>
source.Window(
()=>Observable.Merge(Observable.Timer(bufferPeriod), bufferFlush))
.Select(window=>window.ToList())
.Dump();
//Simulate calling flush.
Observable.Interval(TimeSpan.FromMilliseconds(1350)).Take(2).Subscribe(bufferFlush);