Tunko Development Diary

RxSwift) Transforming Operators (startWith, concat) 본문

Development/RxSwift

RxSwift) Transforming Operators (startWith, concat)

Tunko 2022. 6. 4. 14:10

startWith

startWith는 옵저버블 앞수분에 다른 항목을 추가합니다.

startWith는 가변 파라미터 입니다.

let disposeBag = DisposeBag()
let emoticons = ["🍎", "🍊", "🥝", "🍉"]

Observable.from(emoticons)
    .startWith("🍇")
    .startWith("🍈")
    .startWith("🍌", "🫐", "🍒")
    .subscribe { print($0) }
    .disposed(by: disposeBag)

출력

next(🍌)
next(🫐)
next(🍒)
next(🍈)
next(🍇)
next(🍎)
next(🍊)
next(🥝)
next(🍉)
completed

결과를 보면 가장 나중에 추가한 startWith가 가장 먼저 방출되는것을 확인할 수 있습니다.

concat

concat연산자는 두개의 옵저버블을 연결할 때 사용합니다.

let disposeBag = DisposeBag()
let numbers1_5 = Observable.from([1,2,3,4,5])
let numbers6_10 = Observable.from([6,7,8,9,10])

Observable.concat([numbers1_5, numbers6_10])
    .subscribe{ print($0) }
    .disposed(by: disposeBag)
 
numbers1_5.concat(numbers6_10)
    .subscribe{ print($0) }
    .disposed(by: disposeBag)

출력

next(1)
next(2)
next(3)
next(4)
next(5)
next(6)
next(7)
next(8)
next(9)
next(10)
completed
next(1)
next(2)
next(3)
next(4)
next(5)
next(6)
next(7)
next(8)
next(9)
next(10)
completed
반응형
Comments