rxSwift์ ๋ํ ๊ฐ์๋ฅผ ๋ฃ๋ค๊ฐ ์์ฃผ ์ฌ์ฉํ๋ operator์ ๋ํด ์ ๋ฆฌํด ๋์ผ๋ฉด ์ข์ ๊ฒ ๊ฐ๋ค๋ ์๊ฐ์ด ๋ค๋๋ผ๊ตฌ์
๊ทธ๋์ ์ค๋์ ์ ์ฉํ๊ฒ ์ฌ์ฉ๋๋ operator๋ค์ ๋ํด ์ ๋ฆฌํด๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค
๊ณ์ operator์ ๊ดํด ์ฌ์ฉํ ๋๋ง๋ค ์ฌ๊ธฐ์ ์ ๋ฆฌํด ๋์๋ ค๊ตฌ์ !
Combine
์๋ก ๋ค๋ฅธ ์คํธ๋ฆผ์ ํ๋์ ์คํธ๋ฆผ์ผ๋ก ํฉ์น ๋ ์ฌ์ฉํ๋ operator
CombineLatest
๋ observable์์ ์์ดํ ์ ๋ฐฉ์ถํ ๋ ๊ฐ์ฅ ์ต์ ์ ์์ดํ ์ ๊ฒฐํฉํ๊ณ , ๊ฒฐํฉ๋ ์์ดํ ์ ๋ฐํ
let left = PublishSubject()
let right = PublishSubject()
let observable = Observable.combineLatest(left, right, resultSelector: { lastLeft, lastRight in
"(lastLeft) (lastRight)"
})
let disposable = observable.subscribe(onNext: { value in
print(value)
})
left.onNext(5)
left.onNext(3)
right.onNext(2)
right.onNext(1)
left.onNext(99)
์ถ๋ ฅ ๊ฒฐ๊ณผ
3 2
3 1
99 1
๋ observable์ด ๊ฐ๊ฐ ์ต์ด ๋ฐํ๋์์๋ ๋ฌถ์ด ๋ฐฉ์ถํ๋ค.
์ถ์ฒ : https://min-i0212.tistory.com/16
Merge
๋ง๊ทธ๋๋ก ๋ณํฉ์ ๋๋ค. ๋๊ฐ ์ด์์ Observable ์ถ๋ ฅ์ ๊ฒฐํฉํ์ฌ ๋จ์ผ Observable์ฒ๋ผ ์๋ํ๊ฒ ํจ
mergeํ๊ณ ์๋ ์ฌ๋ฌ observable๋ค์ ๋์์ subscribeํ๋ฉด์ ๋จผ์ ์ค๋ ์ด๋ฒคํธ๋ค์ ๋ฐ๋ก ์ ๋ฌํด์ค๋ค.
let left = PublishSubject<String>()
let right = PublishSubject<String>()
let source = Observable.of(left.asObservable(), right.asObservable())
let observable = source.merge()
let disposable = observable.subscribe(onNext: { value in
print(value)
})
var leftValues = ["Berlin", "Munich", "Frankfurt"]
var rightValues = ["Madrid", "Barcelona", "Valencia"]
repeat {
if arc4random_uniform(2) == 0 {
if !leftValues.isEmpty {
left.onNext("Left: " + leftValues.removeFirst())
}
} else if !rightValues.isEmpty {
right.onNext("Right: " + rightValues.removeFirst())
}
} while !leftValues.isEmpty || !rightValues.isEmpty
disposable.dispose()
์ถ๋ ฅ ๊ฒฐ๊ณผ (ํญ์ ๋ค๋ฅด๊ฒ ๋์จ๋ค)
Left: Berlin
Left: Munich
Right: Madrid
Right: Barcelona
Right: Valencia
Left: Frankfurt
์ถ์ฒ: https://rhammer.tistory.com/309
StartWith
Observable ์์ํ๊ธฐ ์ ์ ์ง์ ๋ ํญ๋ชฉ ์ํ์ค๋ฅผ ๋ฐฉ์ถ
let number = Observable.of(2,3,4)
number.startWith(1)
.subscribe(onNext: {
print($0)
}).disposed(by: disposeBag)
Concat
2๊ฐ ์ด์์ observable์ ์์๋๋ก ๋ฌถ์
let start = Observable.of(1,2,3,4)
let end = Observable.of(5, 6, 7, 8)
let observable = Observable.concat(start, end)
observable.subscribe(onNext: {
print($0)
}).disposed(by: disposeBag)
Zip
๋ ๊ฐ ์ด์์ Observable์์ ๋ฐํ ์์๊ฐ ๊ฐ์ ์์ดํ ์ ๋ฌถ์ด ๋ฐํ
enum Weather { case cloudy, sunny }
let left: Observable < Weather > = Observable.of(.sunny, .cloudy, .cloudy, .sunny)
let right = Observable.of("Lisbon", "Copenhagen", "London", "Madrid", "Vienna")
let observable = Observable
.zip(left, right, resultSelector: { (weather, city) in
return "It's \(weather) in \(city)."
})
observable.subscribe(onNext: { print($0) }).disposed(by: disposeBag)
์ถ๋ ฅ ๊ฒฐ๊ณผ
It's sunny in Lisbon.
It's cloudy in Copenhagen.
It's cloudy in London.
It's sunny in Madrid.
์ถ์ฒ : https://rhammer.tistory.com/311?category=649741
WithLastFrom
๋ observable ์ค ์ฒซ๋ฒ์งธ observable์์ ์์ดํ ์ด ๋ฐฉ์ถ๋ ๋๋ง๋ค ๊ทธ ์์ดํ ์ ๋๋ฒ์งธ observable์ ๊ฐ์ฅ ์ต๊ทผ ์์ดํ ๊ณผ ๊ฒฐํฉํด ๋ฐฉ์ถ
let disposeBag = DisposeBag()
let numSubject = PublishSubject<Int>()
let charSubject = PublishSubject<String>()
numSubject
.withLatestFrom(charSubject) { "\($0)\($1)"}
.subscribe(onNext: { print($0) })
.disposed(by:disposeBag)
numSubject.onNext(1) // ์์ดํ
๋ฐฉ์ถ๋์ง ์์ : charSubject์์ ๋ฐฉ์ถ๋ ์์ดํ
์ด ์์ง 1๊ฐ๋ ์์ผ๋ฏ๋ก
charSubject.onNext("A")
numSubject.onNext(2) // 2A
charSubject.onNext("B")
charSubject.onNext("C")
charSubject.onNext("D")
numSubject.onNext(3) // 3D charSubject์์ ๊ฐ์ฅ ์ต๊ทผ ์์ดํ
D์ ๊ฒฐํฉ
numSubject.onNext(4) // 4D
numSubject.onNext(5) // 5D
์ถ๋ ฅ ๊ฒฐ๊ณผ
2A
3D
4D
5D
ํน์ ๊ฐ์ ํน์ ์ด๋ฒคํธ๋ฅผ ํตํด ์ต์ ๊ฐ์ ๋ฐ์์ค๋๋ฐ ์ฌ์ฉ ํ ์ ์๋ค.
์ถ์ฒ : https://jusung.github.io/withLatestFrom/
Sample
๋ observable ์ค ๋๋ฒ์งธ observable์์ ์์ดํ ์ด ๋ฐฉ์ถ๋ ๋๋ง๋ค ์ฒซ๋ฒ์งธ observable๋ด ๊ฐ์ฅ ์ต์ ๊ฐ์ ๋ฐฉ์ถํ๋ค. ๋ฐฉ์ถ๋ ๊ฐ์ด ์๋ค๋ฉด ๊ฐ์ ๊ฐ์ ๋ฐฉ์ถํ์ง ์๋๋ค.
withLatestFrom(_:) ์ distinctUntilChanged()์ ํฉ์ณ ๋์ ๊ฒ
SwitchLatest
๋ ๊ฐ ์ด์์ Observable์์ ๊ฒฐํฉํ๋ ๊ฐ์ฅ ์ต์ ์ ์์ดํ ์ผ๋ก ๋ฐํ๋ฉ๋๋ค.
// 1
let one = PublishSubject<String>()
let two = PublishSubject<String>()
let three = PublishSubject<String>()
let source = PublishSubject<Observable<String>>()
// 2
let observable = source.switchLatest()
let disposable = observable.subscribe(onNext: { value in
print(value)
}).disposed(by: disposedBag)
// 3
source.onNext(one)
one.onNext("Some text from sequence one")
two.onNext("Some text from sequence two")
source.onNext(two)
two.onNext("More text from sequence two")
one.onNext("and also from sequence one")
source.onNext(three)
two.onNext("Why don't you see me?")
one.onNext("I'm alone, help me")
three.onNext("Hey it's three. I win.")
source.onNext(one)
one.onNext("Nope. It's me, one!")
disposable.dispose()
์ถ๋ ฅ ๊ฒฐ๊ณผ
Some text from sequence one
one More text from sequence two
Hey it's three. I win.
Nope. It's me, one!
์ถ์ฒ: https://rhammer.tistory.com/350?category=649741
Debounce
ํ์ด๋จธ๋ฅผ ์ง์ ํด๋๊ณ ํ์ด๋จธ๊ฐ ๋๋ ์์ ์์ ๊ฐ์ฅ ์ต์ ์ ๊ฐ์ ๋ฐํํด์ค๋๋ค.
'IOS๐ > RxSwift' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[RxSwift] Driver๋ ? (feat. bind) (0) | 2022.03.21 |
---|---|
[RxSwift] Traits ์ด๋ (0) | 2022.03.19 |
[RxSwift] Disposable DisposeBag ์ด๋ (0) | 2022.03.17 |
[RxSwift] 2. Subject์ Relay (0) | 2021.11.20 |
[RxSwift ] 1. Observable์ด๋ (0) | 2021.11.15 |
๋๊ธ