当前位置: 首页 > 教程 > RxJava >

RxJava AsyncSubject类

精华
小牛编辑
123浏览
2023-03-14

RxJava AsyncSubject类 介绍

AsyncSubject 发出唯一的最后一个值,后跟一个完成事件或接收到的错误给观察者。

RxJava AsyncSubject类 声明

public final class  AsyncSubject<T>
extends Subject<T>

RxJava AsyncSubject类 示例

package cn.xnip;

import io.reactivex.subjects.AsyncSubject;

public class ObservableTester  {
   public static void main(String[] args) {   
      final StringBuilder result1 = new StringBuilder();
      final StringBuilder result2 = new StringBuilder();         

      AsyncSubject<String> subject =  AsyncSubject.create();
      subject.subscribe(value -> result1.append(value) ); 
      subject.onNext("a"); 
      subject.onNext("b"); 
      subject.onNext("c"); 
      subject.subscribe(value -> result2.append(value)); 
      subject.onNext("d"); 
      subject.onComplete();

      //Output will be d being the last item emitted
      System.out.println(result1);
      //Output will be d being the last item emitted     
      System.out.println(result2);
   }
}

输出结果为: