this question has answer here:
- rxjava delay each item of list emitted 10 answers
i have observable i've created list of objects. each object in list make network request i'd put delay between each item in list space out requests bit. here's snippet of code.
return observable.from(documentgroupmodels).flatmap(new func1<documentgroupmodel, observable<boolean>>() { @override public observable<boolean> call(documentgroupmodel documentgroupmodel) { return refreshdocumentwithuri(documentgroupmodel.geturi(), documentgroupmodel.sectiongroupid, includeexceptions, false); } });
using delay or buffer doesn't quite work scenario far can tell.
you can use combination of zip , interval operator if delay static, can emit item of zip every time configure on interval.
check example
@test public void delaysteps() { long start = system.currenttimemillis(); subscription subscription = observable.zip(observable.from(arrays.aslist(1, 2, 3)), observable.interval(200, timeunit.milliseconds), (i, t) -> i) .subscribe(n -> system.out.println("time:" + (system.currenttimemillis() - start))); new testsubscriber((observer) subscription).awaitterminalevent(3000, timeunit.milliseconds); }
also can create observable list , use concatmap, can use delay every item emitted. maybe solution more elegant , no hacky
@test public void delayobservablelist() { observable.from(arrays.aslist(1, 2, 3, 4, 5)) .concatmap(s -> observable.just(s).delay(100, timeunit.milliseconds)) .subscribe(n -> system.out.println(n + " emitted"), e -> { }, () -> system.out.println("all emitted")); new testsubscriber().awaitterminalevent(1000, timeunit.milliseconds); }
you can see examples of delay here https://github.com/politrons/reactive/blob/master/src/test/java/rx/observables/utils/observabledelay.java
Comments
Post a Comment