관리 메뉴

꿀맛코딩

defualtIfEmpty and switchIfEmpty 본문

공부방/RxKotlin

defualtIfEmpty and switchIfEmpty

soycrab 2019. 7. 29. 15:08

defualtIfEmpty 

어떠한 배출 값도 없을 때 또는 빈 프로듀서가 나타날때 

Default 값을 배출한다. 

 

그림 1 

Code 


Observable.range(0, 10)
.filter { it > 10 }
.defaultIfEmpty(15)
.subscribe{println("Receive $it")}

 

결과

 

그림 2

 

예외

 

만약 필터링에 

Observable.range(0, 10)
.filter { it > 8 }
.defaultIfEmpty(15)
.subscribe{println("Receive $it")}

 

배출되는 값이 있을경우 .defaultIfEmpty(15) 는 출력 되지 않는다. 

 

결과

그림 3 

 

switchIfEmpty

어떠한 배출값도 없는경우 Default Observable을 반환 할수 있다. 

 

Code

 

Observable.range(0, 10)
.filter { it > 10 }
.switchIfEmpty(Observable.range(11, 10))
.subscribe{println("Receive $it")}

 

결과

그림 4

예외 

defaultIfEmpty 와 마찬가지로 배출값이 있을경우는 Default 값이 배출되지 않는다. 

 

 

참고 : http://reactivex.io/documentation/operators/defaultifempty.html 

반응형

'공부방 > RxKotlin' 카테고리의 다른 글

What is BackPressure? (BackPressure 란?)  (0) 2019.07.25
What is ConnectableObservable?  (0) 2019.07.23
observable.from and observable.just  (0) 2019.07.23
cold observable and hot observable  (0) 2019.07.23
What is Observable?  (0) 2019.07.23
Comments