Polidea / RxBluetoothKit

iOS & OSX Bluetooth library for RxSwift
Apache License 2.0
1.41k stars 366 forks source link

Read characteristic not fire if asSingle() is used (but work with take(1)) #361

Open ghost opened 4 years ago

ghost commented 4 years ago

Describe the bug Try to read a value from a characteristic. If you use asSingle() before subscribe it never triggers (onSuccess or onError), but if you use take(1) it triggers To Reproduce Steps to reproduce the behavior:

example: This won't work

peripheral.establishConnection()
    .flatMap { $0.discoverServices([serviceId]) }.asObservable()
    .flatMap { Observable.from($0) }
    .flatMap { $0.discoverCharacteristics([characteristicId])}.asObservable()
    .flatMap { Observable.from($0) }
    .flatMap { $0.readValue() }.asSingle().subscribe( onSuccess: {//do something }, onError: { //check })

This work

peripheral.establishConnection()
    .flatMap { $0.discoverServices([serviceId]) }.asObservable()
    .flatMap { Observable.from($0) }
    .flatMap { $0.discoverCharacteristics([characteristicId])}.asObservable()
    .flatMap { Observable.from($0) }
    .flatMap { $0.readValue() }.take(1).subscribe( onNext: {//do something }, onError: { //check })

Also I notice this behavior:

peripheral.establishConnection()
    .flatMap { $0.discoverServices([serviceId]) }.asObservable()
    .flatMap { Observable.from($0) }
    .flatMap { $0.discoverCharacteristics([characteristicId])}.asObservable()
    .flatMap { Observable.from($0) }
    .flatMap { $0.readValue() }
        .do(onNext: { //this trigger }
        .asSingle().subscribe( onSuccess: {//nothing happened here }, onError: { //nothing happened here  })

Expected behavior Trigger one time with asSingle

Environment:

P.S. I am new to swift, maybe it is related. is it necessary to dispose a Single<> or Observable with take(1) ?