Rxswift: use asSingle()

Created on 16 May 2018  路  3Comments  路  Source: ReactiveX/RxSwift

let a = PublishSubject()
a.asSingle().subscribe(onSuccess: { (_) in
print("asasaa")
}) { (error) in

}.disposed(by: self.disposeBag)
a.onNext(1)

When I call the code above, there is no print.
Is the asSingle() method using errors?

Most helpful comment

I looked at the code for asSingle() ,
found that I needed to send it myself (. Completed)
So I added take(1) in front of asSingle()

All 3 comments

I looked at the code for asSingle() ,
found that I needed to send it myself (. Completed)
So I added take(1) in front of asSingle()

It is required to call take(1) on PublishSubject before calling asSingle:

let subject = PublishSubject<Void>()
subject
    .take(1) // Without `take(1)` there is no success event
    .asSingle()
    .subscribe(onSuccess: { print("success") }, onError: nil)
subject.onNext(())

This is unexpected and may cause bugs.

I looked at the code for asSingle() ,
found that I needed to send it myself (. Completed)
So I added take(1) in front of asSingle()

great! save my time

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jaumard picture jaumard  路  3Comments

marlowcharite picture marlowcharite  路  3Comments

tyregor picture tyregor  路  3Comments

trungp picture trungp  路  3Comments

RobinFalko picture RobinFalko  路  3Comments