iOS/RxSwift
[RxSwift] AsyncSubject
6cess
2024. 7. 26. 20:46
AsyncSubject는 Observable이 emit한 마지막 값만 emit 하며 Observable이 complete된 후에만 emit 된다. 즉, onCompleted 를 기다렸다가 마지막에 emit 받은 값만 emit 한다.
import RxSwift
let disposeBag = DisposeBag()
let asyncSubject1 = AsyncSubject<String>()
asyncSubject1.subscribe(
onNext: { value in
print("Value: \(value)")
},
onError: { error in
print("Error: \(error)")
},
onCompleted: {
print("Completed")
}
).disposed(by: disposeBag)
asyncSubject1.onNext("A")
asyncSubject1.onNext("B")
asyncSubject1.onNext("C")
asyncSubject1.onCompleted()
// Output : C
AsyncSubject는 error가 발생할 경우 어떤 item도 emit하지 않고 error만 전달한 후 종료된다.
https://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