반응형
2. fromArray/fromIterable
fromArray
- 인자로 지정한 Array를 순서대로 통지 Flowable/Observable을 생성
fromIterable
- 인자로 지정한 Iterable을 순서대로 통지 Flowable/Observable을 생성
[소스코드]
import io.reactivex.Flowable;
public class Main {
public static void main(String[] args) {
fromArray();
fromIterable();
}
static void fromArray() { //fromArray
System.out.println("Ex 1)");
String[] arrays = new String[]{"A", "B", "C", "D", "E"};
Flowable<String> flowable = Flowable.fromArray(arrays);
flowable.subscribe(new PrintSubscriber<>());
System.out.println("Ex 2)");
Flowable<String> flowable2 = Flowable.fromArray("A", "B", "C", "D", "E");
flowable2.subscribe(new PrintSubscriber<>());
}
static void fromIterable() { //fromIterable
String[] arrays = {"A", "B", "C", "D", "E"};
List<String> list = Arrays.asList(arrays);
Flowable<String> flowable = Flowable.fromIterable(list);
flowable.subscribe(new PrintSubscriber<>());
}
}
[Subscribe 코드]
import io.reactivex.subscribers.DisposableSubscriber;
public class PrintSubscriber<T> extends DisposableSubscriber<T> {
private String label;
public PrintSubscriber() {
}
public PrintSubscriber(String label) {
this.label = label;
}
@Override
public void onNext(T data) {
String threadName = Thread.currentThread().getName();
String resultMsg = label == null ? (threadName + " : " + data) : (threadName + " : "+ label + " : "+ data);
System.out.println(resultMsg);
}
@Override
public void onError(Throwable throwable) {
String threadName = Thread.currentThread().getName();
String resultMsg = label == null ? (threadName + " : Exception=" + throwable) : (threadName + " : "+ label + " : "+ throwable);
System.out.println(resultMsg);
}
@Override
public void onComplete() {
String threadName = Thread.currentThread().getName();
String resultMsg = label == null ? (threadName + " : Complete") : (threadName + " : "+ label + " : Complete");
System.out.println(resultMsg);
}
}
[결과]
Ex 1)
main : A
main : B
main : C
main : D
main : E
main : Complete
Ex 2)
main : A
main : B
main : C
main : D
main : E
main : Complete
Iterable
main : A
main : B
main : C
main : D
main : E
main : Complete
반응형
'Language > Java' 카테고리의 다른 글
[RxJava] range, rangeLong (0) | 2023.04.26 |
---|---|
[RxJava] fromCallable (0) | 2023.04.26 |
[RxJava] Just (0) | 2023.04.26 |
[Reactive Programming] RxJava 리소스 관리 (0) | 2023.04.26 |
[Reactive Programming] RxJava 예외 처리 (0) | 2023.04.26 |