📜  RxJava-可连接运算符

📅  最后修改于: 2020-11-12 05:30:11             🧑  作者: Mango


以下是可以更精确地控制订阅的运算符。

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.

可连接运算符示例

使用您选择的任何编辑器(例如,C:\> RxJava)创建以下Java程序。

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 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);
   }
}

验证结果

使用javac编译器编译类,如下所示:

C:\RxJava>javac ObservableTester.java

现在如下运行ObservableTester-

C:\RxJava>java ObservableTester

它应该产生以下输出-

0
7
abcdefg