์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- String.Index
- MongoDB
- DispatchQueue
- ์ง๋ฒ๋ณํ
- nodejs
- AVAudioPlayer
- Codable
- SWiFT
- Firestore
- ios
- ExpressJS
- ์๊ณ ๋ฆฌ์ฆ
- Reduce
- map
- Decodable
- Figma
- Encodable
- Firestore CRUD
- Cloud Firestore
- RxSwift
- UIRefreshController
- Mac
- components
- ์ฑ๋์์ธ
- ํ๋ก๊ทธ๋๋จธ์ค
- Core Data
- Filter
- mongoose
- ์ฑ๋ง๋ค๊ธฐ
- CRUD
- Today
- Total
Focus On Develop ๐ค๐ค
[RxSwift] Subject ๋ณธ๋ฌธ
์ด์ ํฌ์คํ ์์ RxSwift ์ ๋ํ ๊ธฐ๋ณธ์ ์ธ ๊ฐ๋ ๊ณผ ์ฌ์ฉ๋ฒ์ ์์๋ดค๋๋ฐ์~!!
๊ทผ๋ฐ ํ๊ฐ์ง ์๋ฌธ์ ์ด ์๊ธฐ์ฃ !
Observable์ ๊ตฌ๋ ํ๋ฉด์ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌ๋ฐ๊ธฐ๋ ํ๋๋ฐ, ๋ฐ๋๋ก Observable์ ๋ฐ์ดํฐ ๋ณํ๋ ์ด๋ป๊ฒ ์ค ์ ์์๊น์?
๊ทธ๋์ผ ๋ณ๊ฒฝ๋ ๋ฐ์ดํฐ๋ฅผ ๋ค์ ๊ตฌ๋ ํด์ User Interaction์ ๋ฐ๋ผ ๋ญ๊ฐ UI์ ๋ณํ๋ฅผ ์คํ ๋ฐ์!
์ฌ๊ธฐ์ ๋์จ๊ฒ ๋ฐ๋ก Subject ์ ๋๋ค. ํ๋ฒ Subject์ ๋ํด ์์๋ณผ๊น์~~~ ๐
reactivex.io/documentation/subject.html
ReactiveX - Subject
If you have a Subject and you want to pass it along to some other agent without exposing its Subscriber interface, you can mask it by calling its asObservable method, which will return the Subject as a pure Observable. See Also
reactivex.io
๊ณต์ ๋ฌธ์์ Subject์ ๋ํด ์ ๋์์๋๋ฐ, ๊ฐ๋จํ๊ฒ ์ ๋ฆฌํด๋ณผ๊ฒ์!!
Subject๋ Observable์ฒ๋ผ ๊ฐ์ ๋ฐฐ์ถํ ์๋ ์์ง๋ง, ๊ตฌ๋ ๋ ํ ์ ์์ด์. ์ฆ Observable์ด๋ฉด์ Observer์ด๊ธฐ๋ ํ๊ฑฐ์ฃ .
Subject๋ ๋์ ์ผ๋ก ๊ฐ์ Control ๋ฐ ๋ฐฐ์ถํ ์ ์๊ณ , ๊ตฌ๋ ํ๊ณ ์๋ "Observer๋ค"์๊ฒ ๊ฐ์ ๋ฐฐ์ถํฉ๋๋ค. (muticast ๋ฐฉ์)
Observable๊ณผ ์ฐจ์ด์ ์ ๊ตฌ๋ ์ค์ธ ํ๋์ Observer์๊ฒ๋ง ๊ฐ์ ๋ฐฐ์ถํ๋ค๋ ์ฐจ์ด์ ์ด ์์ด์. (unicast ๋ฐฉ์)
Subject๋ 4๊ฐ์ง ์ข ๋ฅ๊ฐ ์์ด์.
PublishSubject
๊ฐ์ฅ ์ผ๋ฐ์ ์ผ๋ก ์ฌ์ฉํ๋ Subject์์.
Subject์ ๋ฐ์ดํฐ๊ฐ ์์ฑ๋๋ฉด ํด๋น Subject๋ฅผ ๊ตฌ๋ ํ๋ Observer๋ค์๊ฒ ๋ฐ์ดํฐ๋ฅผ ๋ฐฐ์ถํด์ฃผ๋ ๋ฐฉ์์ผ๋ก ๋์ํฉ๋๋ค.
** ์์
let publishSubject = PublishSubject<Int>()
publishSubject
.debug()
.subscribe { print("first subscribe: \($0)") }
.disposed(by: disposeBag)
publishSubject.onNext(1)
publishSubject.onNext(2)
publishSubject
.debug()
.subscribe { print("second subscribe: \($0)") }
.disposed(by: disposeBag)
publishSubject.onNext(3)
publishSubject.onNext(4)
publishSubject.onCompleted()
ํ๋์ subject๊ฐ ๊ตฌ๋ ํ ๋ 1,2 ์ด๋ฒคํธ๋ฅผ ๋ฐฐ์ถํ๊ณ , ๋๋ฒ์งธ subject๊ฐ ๊ตฌ๋ ์ ์์ํ์ด์.
๋๋ฒ์งธ subject๋ ๊ตฌ๋ ์์ ์ดํ์ ๋ฐ์ํ ์ด๋ฒคํธ์ธ 3,4๋ง ์์ ํ ๊ฑธ ๋ณผ ์ ์์ด์.
BehaviorSubject
๊ธฐ๋ณธ๊ฐ์ ํ๋ ๊ฐ์ง๊ณ ์์ํด์. ์ด๋ค Observer๊ฐ ๊ตฌ๋ ํ์๋ง์ ๊ธฐ๋ณธ๊ฐ์ ๋ฐ๋ก ๋ฐฐ์ถํด์ค์.
๋ง์ฝ ์ค๊ฐ์ ์๋ก์ด Observer๊ฐ ๊ตฌ๋ ํ๊ฒ ๋๋ฉด, ๊ฐ์ง๊ณ ์๋ ์ต์ ๋ฐ์ดํฐ๋ฅผ ๋ฐ๋ก ๋ฐฐ์ถํด์ฃผ๋ ๋ฐฉ์์ผ๋ก ๋์ํด์.
** ์์
let behaviorSubject = BehaviorSubject<Int>(value: 100)
behaviorSubject
.debug()
.subscribe { print("first subscribe: \($0)") }
.disposed(by: disposeBag)
behaviorSubject.onNext(1)
behaviorSubject.onNext(2)
behaviorSubject
.debug()
.subscribe { print("second subscribe: \($0)") }
.disposed(by: disposeBag)
behaviorSubject.onNext(3)
behaviorSubject.onNext(4)
behaviorSubject.onCompleted()
์์๋๋ก๋ค์!!
์ด๊ธฐ๊ฐ 100์ ๊ฐ์ง๊ณ ์์ํ๊ณ , ์ฒซ๋ฒ์งธ subject๊ฐ ๊ตฌ๋ ํ์๋ง์ 100 ์ด๋ฒคํธ๋ฅผ ๋ฐฐ์ถํด์คฌ์ต๋๋ค.
๋ง์ฐฌ๊ฐ์ง๋ก ๋๋ฒ์งธ subject๊ฐ ๊ตฌ๋ ์ ํ์๋ง์ ๊ฐ์ง๊ณ ์๋ ์ต์ ๋ฐ์ดํฐ์ธ 2๋ฅผ ๋ฐ๋ก ๋ฐฐ์ถํด์คฌ์ด์!!
AsyncSubject
์ฌ๋ฌ Observer๊ฐ ๊ตฌ๋ ํ๊ณ ์๋ ๋์ค์๋, ์ค๊ฐ์ ๋ฐ์ดํฐ๊ฐ ๋ฐ์ํ๋ค๊ณ ๋ฐ๋ก ๋ฐฐ์ถํด์ฃผ์ง ์์์.
๋์ , ๋ง์ง๋ง์ Completed ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ์๋ ๊ฐ์ฅ ๋ง์ง๋ง์ ๋ฐ์ํ ๋ฐ์ดํฐ๋ง ๋ฐฐ์ถํ๋ ๋ฐฉ์์ผ๋ก ๋์ํด์.
** ์์
func testAsyncSubject() {
let asyncSubject = AsyncSubject<Int>()
asyncSubject
.debug()
.subscribe { print($0) }
.disposed(by: disposeBag)
asyncSubject.onNext(1)
asyncSubject.onNext(2)
asyncSubject.onNext(3)
asyncSubject.onCompleted()
}
๊ตฌ๋ ํ๋ ๋์ค์ 1,2,3 ๋ฐ์ดํฐ์ ๋ํ Next ์ด๋ฒคํธ๊ฐ 3๋ฒ ๋ฐ์ํ์ง๋ง, ์ค์ ๋ก๋ ๋ง์ง๋ง์ complete ์ด๋ฒคํธ๊ฐ ํธ์ถ๋ ๋ ๋ง์ง๋ง์ ์์ฑ๋ 3๋ง next์ด๋ฒคํธ๋ก ๋ฐฐ์ถ๋๋ ๊ฑธ ๋ณผ ์ ์์ด์.
ReplaySubject
๊ธฐ๋ณธ์ ์ธ ๋์์ PublishSubject์ ์ ์ฌํ๋ฐ, ์ค๊ฐ์ ๋ค๋ฅธ Observer๊ฐ ๊ตฌ๋ ํ๊ธฐ ์์ํ๋ฉด ์ง๊ธ๊น์ง ๋ฐ์ํ๋ ๋ชจ๋ ๋ฐ์ดํฐ๋ฅผ Buffer Size(๋ช๊ฐ๋ ๋ฐฐ์ถํด์ค๊ฑด์ง) ๋ฐฐ์ถํด์ฃผ๊ณ , ์ดํ๋ถํฐ๋ ๋ฐ์ดํฐ๊ฐ ๋ฐ์ํ ๋๋ง๋ค ๋ฐฐ์ถํด์ฃผ๋ ๋ฐฉ์์ผ๋ก ๋์ํฉ๋๋ค.
** ์์
let replaySubject = ReplaySubject<Int>.create(bufferSize: 2)
replaySubject
.debug()
.subscribe { print("first subscribe: \($0)") }
.disposed(by: disposeBag)
replaySubject.onNext(1)
replaySubject.onNext(2)
replaySubject.onNext(3)
replaySubject
.debug()
.subscribe { print("second subscribe: \($0)") }
.disposed(by: disposeBag)
replaySubject.onNext(4)
replaySubject.onCompleted()
๋ค~!! ์ด๊ฒ๋ ์์๋๋ก ์ ๋๋ค ๐
์ฒซ๋ฒ์งธ subject๊ฐ ๊ตฌ๋ ์ ์์ํ๊ณ 1,2,3 ๋ฐ์ดํฐ๋ฅผ ๋ฐฐ์ถํด์คฌ์ด์. ๊ทธ๋ ๋๋ฒ์งธ subject๊ฐ ๊ตฌ๋ ์ ์์ํ๊ณ , ์ง๊ธ๊น์ง ๋ฐ์ํ๋ ๋ฐ์ดํฐ๋ฅผ ๋ชจ๋ ์์ ํ๊ฒ ๋๋๋ฐ์! ์ฌ๊ธฐ์ ์์ฑ์์ ์ Buffer ํฌ๊ธฐ๋ฅผ 2๋ก ๋ง๋ค์ด ์คฌ์ผ๋, ์ค์ ๋ก 1~3์ 3๊ฐ์ ๋ฐ์ดํฐ๊ฐ ๋ฐ์ํ์์ง๋ง ์ต์ 2๊ฐ์ธ 2,3์ ๋ํ ์ด๋ฒคํธ๋ง ์์ ํ๋ ๊ฑธ ๋ณผ ์ ์์ด์!
์ด๋ฒ ํฌ์คํ ์์๋ Subject์ ๊ฐ๋ ๊ณผ ์ข ๋ฅ, ์ข ๋ฅ๋ณ๋ก ์ด๋ป๊ฒ ๋์ํ๋์ง ํ๋ฒ ์์๋ณด์์ต๋๋ค ๐
์ค๋๋ ๋๊ตฐ๊ฐ์๊ฒ ๋์์ด ๋์๊ธธ ๋ฐ๋์ ๐
'iOS [Swift] > ๊ธฐ์ด๋ฅผ ํํํ!' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Swift] Error Handling (0) | 2021.07.27 |
---|---|
[Swift] Bento #1. iPhone ์ฐ๋ฝ์ฒ ๊ฐ์ ธ์ค๊ธฐ (0) | 2021.04.27 |
[RxSwift] RxSwift ์ฅ๋จ์ (0) | 2021.03.30 |
[RxSwift] RxSwift ์ ๋ฌธํ๊ธฐ (4) | 2021.03.30 |
[Swift] Class vs Struct (0) | 2021.03.16 |