5

有没有办法强制 Observable.Buffer 在缓冲时间结束之前刷新?

在示例中:

mSubscription = mFluxObservable.Buffer(new TimeSpan(0, 0, 1, 30)).Subscribe(o => saver(o, iSessionId));

我想在 1:30 结束之前刷新数据!

4

2 回答 2

9

这对我有用:

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)强制缓冲区刷新。刷新后立即启动一个新缓冲区。

于 2012-10-17T23:21:59.427 回答
3

...和基本上相同的原则使用窗口:

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);
于 2012-10-18T10:39:15.183 回答