June 2017
Intermediate to advanced
400 pages
8h 44m
English
You can use buffer() at fixed time intervals by providing a long and TimeUnit. To buffer emissions into a list at 1-second intervals, you can run the following code. Note that we are making the source emit every 300 milliseconds, and each resulting buffered list will likely contain three or four emissions due to the one-second interval cut-offs:
import io.reactivex.Observable;import java.util.concurrent.TimeUnit;public class Launcher { public static void main(String[] args) { Observable.interval(300, TimeUnit.MILLISECONDS) .map(i -> (i + 1) * 300) // map to elapsed time .buffer(1, TimeUnit.SECONDS) .subscribe(System.out::println); sleep(4000); } public static void sleep(int millis) { try { Thread.sleep(millis); } catch ...Read now
Unlock full access