blockingMostRecent()

The blockingMostRecent() is similar to blockingLatest(), but it will re-consume the latest value repeatedly for every next() call from the iterator even if it was consumed already. It also requires a defaultValue argument so it has something to return if no value is emitted yet. Here, we use blockingMostRecent() against an Observable emitting every 10 milliseconds. The default value is -1, and it consumes each value repeatedly until the next value is provided:

 import io.reactivex.Observable;
import org.junit.Test;
import java.util.concurrent.TimeUnit;

public class RxTest {

@Test
public void testBlockingMostRecent() {
Observable<Long> source =
Observable.interval(10, TimeUnit.MILLISECONDS)
.take(5);

Iterable<Long> iterable = source.blockingMostRecent(-1L);

for (Long i: iterable) {
System.out.println(i);
}
}
}

The output is as follows:

-1
-1
-1
...
0
0
0
...
1
1
1
...

As we finish covering blocking operators, it should be emphasized again that they can be an effective way to do simple assertions and provide means to block for results so they can be consumed easily by a testing framework. However, you will want to avoid using blocking operators for production as much as possible. Try not to give into the sirens of convenience, as you will find that they can quickly undermine the flexibility and benefits of reactive programming.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.137.2