following are the operators which has more precisely control over subscription.
sr.no. | operator & description |
---|---|
1 |
connect instruct a connectable observable to emit items to its subscribers. |
2 |
publish converts an observable to connectable observable. |
3 |
refcount converts a connectable observable to ordinary observable. |
4 |
replay ensure same sequence of emitted items to be seen by each subscriber, even after the observable has begun emitting items and subscribers subscribe later. |
connectable operator example
create the following java program using any editor of your choice in, say, c:\> rxjava.
observabletester.java
import io.reactivex.observable; import io.reactivex.observables.connectableobservable; //using connect operator on a connectableobservable public class observabletester { public static void main(string[] args) { string[] letters = {"a", "b", "c", "d", "e", "f", "g"}; final stringbuilder result = new stringbuilder(); connectableobservable<string> connectable = observable.fromarray(letters).publish(); connectable.subscribe(letter -> result.append(letter)); system.out.println(result.length()); connectable.connect(); system.out.println(result.length()); system.out.println(result); } }
verify the result
compile the class using javac compiler as follows −
c:\rxjava>javac observabletester.java
now run the observabletester as follows −
c:\rxjava>java observabletester
it should produce the following output −
0 7 abcdefg