티스토리 뷰
1. Observable의 라이프사이클
Observable.just(1).debug("just").subscribe().dispose()
2019-04-04 12:14:44.633: just -> subscribed
2019-04-04 12:14:44.703: just -> Event next(1)
2019-04-04 12:14:44.710: just -> Event completed
2019-04-04 12:14:44.710: just -> isDisposed
2. Observable의 공유
- 옵저버블을 공유하지 않으면 subscribe 횟수 만큼 이벤트가 발생한다
- API Call을 하는 Observable을 두번 subscribe하면 API Call이 두번 일어난다
2.1 Observable을 공유하지 않았을 때
let observable = Observable<Int>.interval(1, scheduler: MainScheduler.instance).take(2).map { (element) -> Int in
print("map : \(element)")
return element
}
observable.subscribe(onNext: { (element) in
print("observable subscribe1 : \(element)")
}).disposed(by: disposeBag)
observable.subscribe(onNext: { (element) in
print("observable subscribe2 : \(element)")
}).disposed(by: disposeBag)
map : 0
observable subscribe1 : 0
map : 0
observable subscribe2 : 0
map : 1
observable subscribe1 : 1
map : 1
observable subscribe2 : 1
=> map이 observable마다 2개씩 출력되는 것을 보니 observable이 공유되지 않고 있음!!
2.2 ConnectableObservable
extension ObservableType {
/**
Returns a connectable observable sequence that shares a single subscription to the underlying sequence.
This operator is a specialization of `multicast` using a `PublishSubject`.
- seealso: [publish operator on reactivex.io](http://reactivex.io/documentation/operators/publish.html)
- returns: A connectable observable sequence that shares a single subscription to the underlying sequence.
*/
public func publish() -> ConnectableObservable<E> {
return self.multicast { PublishSubject() }
}
}
이 publish 메소드를 사용해서 ConnectableObservable을 만들 수 있다
let observable = Observable<Int>.interval(1, scheduler: MainScheduler.instance).take(2).map { (element) -> Int in
print("map : \(element)")
return element
}
let connectableObservable: ConnectableObservable<Int> = observable.publish()
connectableObservable.subscribe(onNext: { (element) in
print("connectableObservable subscribe1 : \(element)")
}).disposed(by: disposeBag)
connectableObservable.subscribe(onNext: { (element) in
print("connectableObservable subscribe2 : \(element)")
}).disposed(by: disposeBag)
// connect해줘야지 시퀀스가 시작됨--!
connectableObservable.connect().disposed(by: disposeBag)
map : 0
connectableObservable subscribe1 : 0
connectableObservable subscribe2 : 0
map : 1
connectableObservable subscribe1 : 1
connectableObservable subscribe2 : 1
=> observable이 공유되고 있음!!
+ 공유에 대하여 더 많은 정보를 담고 있는 블로그 : https://brunch.co.kr/@tilltue/15
'🍏 > RxSwift' 카테고리의 다른 글
[RxSwift] Subject / Relay / Driver / Variable (2) | 2019.04.04 |
---|---|
[RxSwift] Subject (0) | 2019.04.04 |
[RxSwift] Rx로 TableView를 그리는 4가지 방법 + setDelegate (2) | 2019.03.18 |
[RxSwift] 예제로 이해하는 Delegate Proxy (4) | 2019.02.04 |
[Rxswift] RGB Slider로 View의 Color를 바꿔주기 (0) | 2018.07.24 |
- Total
- Today
- Yesterday
- cocoapod
- Flutter Spacer
- 장고 URL querystring
- Flutter Clipboard
- Python Type Hint
- Django Firebase Cloud Messaging
- ipad multitasking
- METAL
- DRF APIException
- flutter build mode
- drf custom error
- 구글 Geocoding API
- Flutter Text Gradient
- 플러터 싱글톤
- SerializerMethodField
- flutter dynamic link
- PencilKit
- Sketch 누끼
- flutter 앱 출시
- flutter deep link
- Flutter 로딩
- Dart Factory
- ribs
- Django Heroku Scheduler
- github actions
- Watch App for iOS App vs Watch App
- 장고 Custom Management Command
- Flutter getter setter
- 플러터 얼럿
- Django FCM
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |