반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Operators
- SwiftUI
- @Environment
- vim
- IOS
- NavigationLink
- dismiss
- RxSwift
- subject
- @EnvironmentObject
- typeorm
- graphql
- nonisolated
- init?
- operator
- nestjs
- init
- RFC1738/1808
- RxCocoa
- Creating Operators
- @State
- SWIFT
- ios14
- NullObject
- URL(string:)
- Xcode
- Bug
- Operater
- swift6
- @Binding
Archives
- Today
- Total
Tunko Development Diary
RxSwift) sharing Subscription Operator (refCount) 본문
RefCount
extension ConnectableObservableType {
public func refCount() -> Observable<Element> {
RefCount(source: self)
}
}
RefCount 는 ConnectableObservableType 형에만 따로 구현되어있습니다. 구현된것을 보면 파라미터는 없고 Observable<Element> 을 리턴합니다.
RefCount는 옵저버블입니다.
내부에 ConnectableObservable을 유지하면서 새로운 구독자가 생성되는 시점에 자동으로 connect() 시켜줍니다.
let disposeBag = DisposeBag()
let oneSecondObservable = Observable<Int>.interval(.seconds(1), scheduler: MainScheduler.instance)
.take(5)
.publish()
.refCount()
.debug()
let observer1 = oneSecondObservable.subscribe { print("🍎 : ", $0) }
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
observer1.dispose()
}
DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
let observer2 = oneSecondObservable
.subscribe { print("🥝 : ", $0) }
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
observer2.dispose()
}
}
출력
2022-06-08 11:28:40.158: refCount.playground:59 (__lldb_expr_88) -> subscribed
2022-06-08 11:28:41.162: refCount.playground:59 (__lldb_expr_88) -> Event next(0)
🍎 : next(0)
2022-06-08 11:28:42.162: refCount.playground:59 (__lldb_expr_88) -> Event next(1)
🍎 : next(1)
2022-06-08 11:28:43.161: refCount.playground:59 (__lldb_expr_88) -> Event next(2)
🍎 : next(2)
2022-06-08 11:28:43.298: refCount.playground:59 (__lldb_expr_88) -> isDisposed
2022-06-08 11:28:45.297: refCount.playground:59 (__lldb_expr_88) -> subscribed
2022-06-08 11:28:46.299: refCount.playground:59 (__lldb_expr_88) -> Event next(0)
🥝 : next(0)
2022-06-08 11:28:47.298: refCount.playground:59 (__lldb_expr_88) -> Event next(1)
🥝 : next(1)
2022-06-08 11:28:48.298: refCount.playground:59 (__lldb_expr_88) -> Event next(2)
🥝 : next(2)
출력 로그를 보면 🍎 옵저버블이 isDisposed 된후에 🥝 옵저버블이 다시 구독을 시작해서 새로운 시퀀스로 이벤트를 방출합니다
반응형
'Development > RxSwift' 카테고리의 다른 글
RxSwift) Scheduler (0) | 2022.06.09 |
---|---|
RxSwift) sharing Subscription Operator (share) (0) | 2022.06.08 |
RxSwift) sharing Subscription Operator (multicast, publish, replay) (0) | 2022.06.08 |
RxSwift) Time-based Operators(delay, delaySubscription) (0) | 2022.06.07 |
RxSwift) Time-based Operators(timer, timeout) (0) | 2022.06.07 |
Comments