【问题标题】:OnNext not being called on observable when using combineLatest and takeOnNext 在使用 combineLatest 和 take 时不会被 observable 调用
【发布时间】:2017-12-07 15:03:55
【问题描述】:

我正在尝试订阅以在它发出项目时自动取消订阅。基本 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

【问题讨论】:

  • 什么是ConnectionSharingAdapter。个人readCharacteristics 生产任何物品吗?将doOnNext 放在各个地方,看看事件在哪里消失。
  • 连接共享适配器是RxAndroidBle 的一部分。由于每次订阅都会创建一个与设备的连接,因此连接共享适配器基本上确保一次只有一个处于活动状态。我会试试doOnNext

标签: rx-java2 rxandroidble unsubscribe combinelatest


【解决方案1】:

我需要在flatMap 之前和之后调用take(1)。这篇文章有点解释它Read multiple characteristics from an Android device using library RxAndroidBle

【讨论】:

  • 根据我对RxJava 的了解,情况并非如此,原始帖子中的代码应该可以按预期工作——应该调用一个订阅操作。如果您创建Observable.combineLatest 的部分被调用,您能否添加RxBleLog.setLogLevel(RxBleLog.VERBOSE) 和更多日志记录?您遇到的行为一定有某种原因。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-25
  • 2016-10-03
  • 2020-08-05
  • 1970-01-01
  • 2021-03-06
  • 1970-01-01
相关资源
最近更新 更多