๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“Œ Language/Swift

[RxSwift] Combining Observables ์•Œ์•„๋ณด๊ธฐ (Combine Latest,Zip, Merge,Concat,withLatestFrom)

by Fomagran ๐Ÿ’ป 2021. 4. 7.
728x90
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š” Foma ์ž…๋‹ˆ๋‹ค!

 

์˜ค๋žœ๋งŒ์— RxSwift ๊ธ€์„ ํฌ์ŠคํŒ…์„ ํ•˜๋Š”๋ฐ.. ์จ์•ผ์ง€ ์จ์•ผ์ง€ ๋ฏธ๋ฃจ๋‹ค๊ฐ€ ์“ฐ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค...

 

์˜ค๋Š˜์€ ์—ฌ๋Ÿฌ๊ฐœ์˜ ์ด๋ฒคํŠธ๋“ค์„ ํ•ฉ์ณ์ฃผ๋Š” ์—ฐ์‚ฐ์ž๋“ค์„ ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!

๋ฐ”๋กœ ์‹œ์ž‘ํ• ๊ฒŒ์š”~


1.CombineLatest

 

์—ฌ๋Ÿฌ๊ฐœ์˜ ์˜ต์ €๋ฒ„๋ธ”์˜ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์˜ ๊ฐ’์„ ํ•ฉ์ณ์„œ ํ•˜๋‚˜์˜ ๊ฒฐ๊ณผ๋กœ ๋ฐฉ์ถœํ•ฉ๋‹ˆ๋‹ค.

 

๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฐ๊ฐ ์ƒ‰๊น”,๋ชจ์–‘,์ˆซ์ž๋ฅผ ๋ฐฉ์ถœํ•˜๋Š” ์„œ๋ธŒ์ ํŠธ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

let ์ƒ‰๊น” = PublishSubject<String>()
let ๋ชจ์–‘ = PublishSubject<String>()
let ์ˆซ์ž = PublishSubject<Int>()

์ปด๋ฐ”์ธ๋ ˆ์ดํ‹ฐ์ŠคํŠธ๋กœ ๊ฐ ์˜ต์ €๋ฒ„๋ธ”์„ ๋ฌถ์€ ๋’ค ๊ตฌ๋…์„ ํ•˜์—ฌ ๋ฐฉ์ถœํ•˜๋Š” ๊ฐ’์„ ์ถœ๋ ฅํ•ด๋ด…๋‹ˆ๋‹ค.

let combineLatest = Observable.combineLatest(์ƒ‰๊น”,๋ชจ์–‘,์ˆซ์ž) {์ƒ‰๊น”,๋ชจ์–‘,์ˆซ์ž in "\(์ƒ‰๊น”),\(๋ชจ์–‘),\(์ˆซ์ž)"}
combineLatest.subscribe({print($0)})

๋งŒ์•ฝ ์ด๋ ‡๊ฒŒ ๊ฒ€์ •์ƒ‰์ธ ์ƒ‰๊น”์„ ๋ฐฉ์ถœํ•˜๋ฉด ์–ด๋–ค ๊ฐ’์ด ์ถœ๋ ฅ๋ ๊นŒ์š”?

 

์ •๋‹ต์€ ์•„๋ฌด๊ฒƒ๋„ ๋ฐฉ์ถœ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

 

3๊ฐœ์˜ ์˜ต์ €๋ฒ„๋ธ”์˜ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ๊ฐ’์„ ๋ฌถ์–ด์„œ ๋งŒ๋“œ๋Š” ๊ฐ’์ด๋ฏ€๋กœ ์ตœ์†Œ ๊ฐ๊ฐ์˜ ์˜ต์ €๋ฒ„๋ธ”๋ณ„๋กœ ํ•˜๋‚˜์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐฉ์ถœํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

์ƒ‰๊น”.onNext("๊ฒ€์ •์ƒ‰")

์œ„์˜ ์ƒ‰๊น”๊ณผ ๋ชจ์–‘ ์ˆซ์ž ์ด๋ ‡๊ฒŒ 3๊ฐœ์˜ ์˜ต์ €๋ฒ„๋ธ”์ด ๊ฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐฉ์ถœํ•˜๋ฉด ๊ฒ€์ •์ƒ‰,๋™๊ทธ๋ผ๋ฏธ,1์ด ํ•˜๋‚˜๋กœ ํ•ฉ์ณ์ ธ์„œ ๋ฐฉ์ถœ๋˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

๋ชจ์–‘.onNext("๋™๊ทธ๋ผ๋ฏธ")
์ˆซ์ž.onNext(1)
//next(๊ฒ€์ •์ƒ‰,๋™๊ทธ๋ผ๋ฏธ,1)

์—ฌ๊ธฐ์„œ ์ƒ‰๊น”์ด ํŒŒ๋ž€์ƒ‰์œผ๋กœ ๋ฐฉ์ถœ๋˜๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ์š”?

 

๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์— ๋ฐฉ์ถœ๋œ ๋ชจ์–‘๊ณผ ์ˆซ์ž๊ฐ€ ํ•ฉ์ณ์ ธ ํŒŒ๋ž€์ƒ‰ ๋™๊ทธ๋ผ๋ฏธ 1๋กœ ๋ฐฉ์ถœ๋˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

์ƒ‰๊น”.onNext("ํŒŒ๋ž€์ƒ‰")
//next(ํŒŒ๋ž€์ƒ‰,๋™๊ทธ๋ผ๋ฏธ,1)

2.Zip

 

์—ฌ๋Ÿฌ๊ฐœ์˜ ์˜ต์ €๋ฒ„๋ธ”์˜ ๊ฐ’์„ ํ•ฉ์ณ์„œ ํ•˜๋‚˜์˜ ๊ฒฐ๊ณผ๋กœ ๋ฐฉ์ถœํ•ฉ๋‹ˆ๋‹ค.

 

์•„๊นŒ์™€ ๊ฐ™์ด ์ƒ‰๊น”๊ณผ ๋ชจ์–‘๊ณผ ์ˆซ์ž๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

let ์ƒ‰๊น” = PublishSubject<String>()
let ๋ชจ์–‘ = PublishSubject<String>()
let ์ˆซ์ž = PublishSubject<Int>()

let zip = Observable.zip(์ƒ‰๊น”,๋ชจ์–‘,์ˆซ์ž)  {์ƒ‰๊น”,๋ชจ์–‘,์ˆซ์ž in "\(์ƒ‰๊น”),\(๋ชจ์–‘),\(์ˆซ์ž)"}
zip.subscribe({print($0)})

์ปด๋ฐ”์ธ ๋ ˆ์ดํ‹ฐ์ŠคํŠธ์™€ ๊ฐ™์ด ๊ฐ ์˜ต์ €๋ฒ„๋ธ”์ด ์ตœ์†Œ ํ•˜๋‚˜๋ผ๋„ ๋ฐฉ์ถœ๋˜์•ผ ์ถœ๋ ฅ์ด ๋ฉ๋‹ˆ๋‹ค.

 

์•„๋ž˜์™€ ๊ฐ™์ด ์ƒ‰๊น”๋งŒ ๋ฐฉ์ถœ๋Œ”์„๋• ์•„๋ฌด๊ฒƒ๋„ ์ถœ๋ ฅ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์ƒ‰๊น”.onNext("๊ฒ€์ •์ƒ‰")

์ด๋ ‡๊ฒŒ ๋ชจ์–‘๊ณผ ์ˆซ์ž๊นŒ์ง€ ๋ฐฉ์ถœํ•ด์ฃผ๋ฉด ๊ฒ€์ •์ƒ‰,๋™๊ทธ๋ผ๋ฏธ,1์ด ๋ฐฉ์ถœ๋˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

๋ชจ์–‘.onNext("๋™๊ทธ๋ผ๋ฏธ")
์ˆซ์ž.onNext(1)
//next(๊ฒ€์ •์ƒ‰,๋™๊ทธ๋ผ๋ฏธ,1)

๊ทธ๋Ÿฌ๋ฉด CombineLatest์™€ ๋‹ค๋ฅธ ์ ์€ ๋ฌด์—‡์ผ๊นŒ์š”?

 

์ƒ‰๊น”์„ ํŒŒ๋ž€์ƒ‰์œผ๋กœ ๋ฐฉ์ถœํ•˜๊ฒŒ ๋˜๋ฉด ์•„๋ฌด๋Ÿฐ ๊ฒƒ๋„ ์ถœ๋ ฅ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์ƒ‰๊น”.onNext("ํŒŒ๋ž€์ƒ‰")

 

์ฆ‰, ์ƒˆ๋กญ๊ฒŒ ๋ฐฉ์ถœ๋œ ๊ฐ ๋ฐ์ดํ„ฐ๋“ค์„ ํ•ฉ์ณ์„œ ํ•˜๋‚˜์˜ ๋ฐ์ดํ„ฐ๋กœ ๋ฐฉ์ถœํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์ƒ‰๊น”.onNext("๊ฒ€์ •์ƒ‰")
๋ชจ์–‘.onNext("๋™๊ทธ๋ผ๋ฏธ")
์ˆซ์ž.onNext(1)
์ƒ‰๊น”.onNext("ํŒŒ๋ž€์ƒ‰")
๋ชจ์–‘.onNext("๋„ค๋ชจ")
์ˆซ์ž.onNext(2)
//next(๊ฒ€์ •์ƒ‰,๋™๊ทธ๋ผ๋ฏธ,1)
//next(ํŒŒ๋ž€์ƒ‰,๋„ค๋ชจ,2)

3.Merge

 

์—ฌ๋Ÿฌ๊ฐœ์˜ ์˜ต์ €๋ฒ„๋ธ”์„ ํ•ฉ์ณ ๊ฐ๊ฐ ๋ฐฉ์ถœ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ชจ๋‘ ๋ฐฉ์ถœํ•ฉ๋‹ˆ๋‹ค.

 

์œ„์™€ ๊ฐ™์ด ์ƒ‰๊น”,๋ชจ์–‘,์ˆซ์ž๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

 

ํ•˜์ง€๋งŒ ๋‹ค๋ฅธ ์ ์€ ์ˆซ์ž๊ฐ€ String์ด์ฃ ?

 

merge๋Š” ๋ฐฉ์ถœ๋˜๋Š” ๋ฐ์ดํ„ฐ ํ˜•์ด ๋ชจ๋‘ ๋˜‘๊ฐ™์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค.

let ์ƒ‰๊น” = PublishSubject<String>()
let ๋ชจ์–‘ = PublishSubject<String>()
let ์ˆซ์ž = PublishSubject<String>()

๊ตฌ๋…ํ•œ ๋’ค ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•ด๋ณด๋ฉด ๋ฌถ์–ด์ค€ ์˜ต์ €๋ฒ„๋ธ”์—์„œ ๋ฐฉ์ถœ๋œ ๋ฐ์ดํ„ฐ ๋ชจ๋‘๋ฅผ ๋ฐฉ์ถœํ•ฉ๋‹ˆ๋‹ค.

let merge = Observable.merge(์ƒ‰๊น”,๋ชจ์–‘,์ˆซ์ž)
merge.subscribe{print($0)}

์ƒ‰๊น”.onNext("๊ฒ€์ •์ƒ‰")
๋ชจ์–‘.onNext("๋™๊ทธ๋ผ๋ฏธ")
์ˆซ์ž.onNext("1")
์ƒ‰๊น”.onNext("ํŒŒ๋ž€์ƒ‰")
//next(๊ฒ€์ •์ƒ‰)
//next(๋™๊ทธ๋ผ๋ฏธ)
//next(1)
//next(ํŒŒ๋ž€์ƒ‰)

4.Concat

 

์—ฌ๋Ÿฌ๊ฐœ์˜ ์˜ต์ €๋ฒ„๋ธ”์ด ๋ฐฉ์ถœํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฐจ๋ก€๋กœ ๋ฐฉ์ถœํ•ฉ๋‹ˆ๋‹ค.

 

 

์ปจ์บฃ๋„ ๋ฉ€์ง€์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ชจ๋“  ํ˜•์ด ๊ฐ™์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

์ƒ‰๊น”,๋ชจ์–‘,์ˆซ์ž๋ฅผ ์ฐจ๋ก€๋กœ ๋ฌถ์—ˆ๋‹ค๋ฉด ์ƒ‰๊น”์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ชจ๋‘ ๋ฐฉ์ถœ๋œ ํ›„ ์ฆ‰, ์ปดํ”Œ๋ฆฟ์ด ๋ฐฉ์ถœ๋œ ์ดํ›„์— ๋ฐฉ์ถœ๋œ ๋ชจ์–‘์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฐฉ์ถœ๋˜๊ณ 

 

๋ชจ์–‘์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์ปดํ”Œ๋ฆฟ์ด ๋˜๊ณ  ์ˆซ์ž์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฐฉ์ถœ๋ฉ๋‹ˆ๋‹ค.

 

์ปดํ”Œ๋ฆฟ์ด ๋˜๊ธฐ ์ „์— ๋ฐฉ์ถœ๋œ ๊ทธ ์ „์˜ ์˜ต์ €๋ฒ„๋ธ” ๋ฐ์ดํ„ฐ๋Š” ๋ฐฉ์ถœ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

let ์ƒ‰๊น” = PublishSubject<String>()
let ๋ชจ์–‘ = PublishSubject<String>()
let ์ˆซ์ž = PublishSubject<String>()

let concat = Observable.concat(์ƒ‰๊น”,๋ชจ์–‘,์ˆซ์ž)
concat.subscribe{print($0)}

์ƒ‰๊น”.onNext("๊ฒ€์ •์ƒ‰")
์ƒ‰๊น”.onNext("ํŒŒ๋ž€์ƒ‰")
๋ชจ์–‘.onNext("๋„ค๋ชจ")
์ƒ‰๊น”.onCompleted()
๋ชจ์–‘.onNext("๋™๊ทธ๋ผ๋ฏธ")
๋ชจ์–‘.onCompleted()
์ˆซ์ž.onNext("1")
//next(๊ฒ€์ •์ƒ‰)
//next(ํŒŒ๋ž€์ƒ‰)
//next(๋™๊ทธ๋ผ๋ฏธ)
//next(1)

5.withLatestFrom

 

๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์— ๋ฐฉ์ถœํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐฉ์ถœํ•ฉ๋‹ˆ๋‹ค.

 

์•„๋ž˜์™€ ๊ฐ™์ด ๋งŒ์•ฝ ๋ฒ„ํŠผ๊ณผ ํ…์ŠคํŠธํ•„๋“œ๊ฐ€ ์žˆ๊ณ  ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„๋•Œ ํ…์ŠคํŠธํ•„๋“œ์˜ ๊ฐ’์„ ๋ฐฉ์ถœํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ• ๋•Œ

 

์šฐ๋ฆฌ๋Š” ํ…์ŠคํŠธํ•„๋“œ์˜ ๋งจ ๋งˆ์ง€๋ง‰ ๋ฐ์ดํ„ฐ๋ฅผ ์›ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

๊ทธ๋Ÿด๋•Œ ์•Œ๋งž๋Š” ์—ฐ์‚ฐ์ž๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

let button = PublishSubject<Bool>()
let textField = PublishSubject<String>()

let withLatestFrom = button.withLatestFrom(textField)
withLatestFrom.subscribe({print($0)})

textField.onNext("S")
textField.onNext("Swi")
button.onNext(true)
textField.onNext("Swif")
textField.onNext("Swift")
button.onNext(true)
//next(Swi)
//next(Swift)

์ด๋ ‡๊ฒŒ ์˜ค๋Š˜์€ ์—ฌ๋Ÿฌ๊ฐœ์˜ ์˜ต์ €๋ฒ„๋ธ”์„ ํ•ฉ์ณ์ฃผ๋Š” ์˜คํผ๋ ˆ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์•˜๋Š”๋ฐ์š”.

 

ํ˜น์‹œ๋ผ๋„ ํ‹€๋ฆฐ ์ ์ด ์žˆ๊ฑฐ๋‚˜ ๊ถ๊ธˆํ•˜์‹  ๋ถ€๋ถ„์ด ์žˆ๋‹ค๋ฉด ๋Œ“๊ธ€๋กœ ์•Œ๋ ค์ฃผ์„ธ์š”!

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€