使用combineLatest和take时,OnNext不会被observable调用

时间:2017-12-07 15:03:55

标签: rx-java2 rxandroidble unsubscribe combinelatest

我正在尝试订阅在发出项目时自动取消订阅。 base observable就是这样创建的。

public static Observable<RxBleConnection> setupConnection(RxBleDevice device, PublishSubject<Void> disconnectTrigger) {
    return device
            .establishConnection(false)
            .takeUntil(disconnectTrigger)
            .retry(3)
            .retryWhen(o -> o.delay(RETRY_DELAY, TimeUnit.MILLISECONDS))
            .compose(new ConnectionSharingAdapter());
}

然后我尝试将三个读取操作组合成ProgramModel

private void readCharacteristics(Action1<ProgramModel> onReadSuccess) {
    mConnectionObservable
            .flatMap(rxBleConnection ->
                    // combines the following three observables into a single observable that is
                    // emitted in onNext of the subscribe
                    Observable.combineLatest(
                            rxBleConnection.readCharacteristic(UUID_SERIAL_NUMBER),
                            rxBleConnection.readCharacteristic(UUID_MACHINE_TYPE),
                            rxBleConnection.readCharacteristic(UUID_CHARACTERISTIC),
                            ProgramModel::new))
            .observeOn(AndroidSchedulers.mainThread())
            .take(1)
            .subscribe(programModel -> {
                programModel.trimSerial();
                onReadSuccess.call(programModel);
            }, BleUtil::logError);
}

因此理论上,一旦程序模型通过订阅的oNext到来,订阅将被取消订阅。由于某种原因,操作卡住了,onNextonError永远不会被调用。如果我删除take(1)这可以正常工作,但我不想处理持有订阅的引用和手动取消订阅。有谁知道我做错了什么或为什么onNext没有被调用?

1 个答案:

答案 0 :(得分:0)

我需要在take(1)之前以及之后致电flatMap。这篇文章解释了它Read multiple characteristics from an Android device using library RxAndroidBle