Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用share():将最后一个值发布到新订阅者 @覆盖 公共无效暂停(){ super.onPause(); compositeDisposable.clear(); } @凌驾 恢复时公开作废(){ super.onResume(); FlowAbable distanceFlowable=myDataProcessor.hide().onBackpressureLatest() .distinctUntilChanged() .share(); compositeDisposable.add(可移动的距离) .observeOn(AndroidSchedulers.mainThread()) .subscribe(data->{Log.w(“观察者1”,data.value)}); compositeDisposable.add(可移动的距离) .observeOn(AndroidSchedulers.mainThread()) .subscribe(data->{Log.w(“观察者2”,data.value)}); }_Android_Rx Java2 - Fatal编程技术网

使用share():将最后一个值发布到新订阅者 @覆盖 公共无效暂停(){ super.onPause(); compositeDisposable.clear(); } @凌驾 恢复时公开作废(){ super.onResume(); FlowAbable distanceFlowable=myDataProcessor.hide().onBackpressureLatest() .distinctUntilChanged() .share(); compositeDisposable.add(可移动的距离) .observeOn(AndroidSchedulers.mainThread()) .subscribe(data->{Log.w(“观察者1”,data.value)}); compositeDisposable.add(可移动的距离) .observeOn(AndroidSchedulers.mainThread()) .subscribe(data->{Log.w(“观察者2”,data.value)}); }

使用share():将最后一个值发布到新订阅者 @覆盖 公共无效暂停(){ super.onPause(); compositeDisposable.clear(); } @凌驾 恢复时公开作废(){ super.onResume(); FlowAbable distanceFlowable=myDataProcessor.hide().onBackpressureLatest() .distinctUntilChanged() .share(); compositeDisposable.add(可移动的距离) .observeOn(AndroidSchedulers.mainThread()) .subscribe(data->{Log.w(“观察者1”,data.value)}); compositeDisposable.add(可移动的距离) .observeOn(AndroidSchedulers.mainThread()) .subscribe(data->{Log.w(“观察者2”,data.value)}); },android,rx-java2,Android,Rx Java2,当distanceFlowable不断得到新的更改时,代码运行良好。然而,在一个场景中,当在可远程流动上只有一个帖子时,只有观察者1得到通知 其行为如下: 可远程流动的post 观察者1订阅 logcat打印“观察者1:133” 观察者2订阅 我希望它的行为像: 可远程流动的post 观察者1订阅 logcat打印“观察者1:133” 观察者2订阅 logcat打印“观察员2:133” 我尝试使用ConnectedFlowable代替publish(),然后在订阅了观察者1和观察者2之后,将co

当distanceFlowable不断得到新的更改时,代码运行良好。然而,在一个场景中,当在可远程流动上只有一个帖子时,只有观察者1得到通知

其行为如下:

  • 可远程流动的post
  • 观察者1订阅
  • logcat打印“观察者1:133”
  • 观察者2订阅
  • 我希望它的行为像:

  • 可远程流动的post
  • 观察者1订阅
  • logcat打印“观察者1:133”
  • 观察者2订阅
  • logcat打印“观察员2:133”
  • 我尝试使用
    ConnectedFlowable
    代替
    publish()
    ,然后在订阅了观察者1和观察者2之后,将
    connect()
    连接到flowable。但是,即使在清除了
    compositeDisposable
    并且没有人监听之后,它仍然会在flowable上发布


    解决这个问题的首选方法是什么?

    我错过了
    可流动。connect()
    返回一个
    一次性的
    ,我可以将它添加到我的
    可组合的
    ,这样它就会在暂停时被清除。

    只需使用
    .compose(ReplayingShare.instance())来自
    


    这应该行得通。

    那么.replay(1).refCount()呢?我也读过这方面的内容,但我无法让它正常工作。这看起来正是我所需要的。我认为他们默认提供了这一点。
    @Override
    public void onPause() {
        super.onPause();
        compositeDisposable.clear();
    }
    
    @Override
    public void onResume() {
        super.onResume();
    
        Flowable<MyData> distanceFlowable = myDataProcessor.hide().onBackpressureLatest()
                .distinctUntilChanged()
                .share();
    
        compositeDisposable.add(distanceFlowable)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(data -> {Log.w("observer 1", data.value)});
    
        compositeDisposable.add(distanceFlowable)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(data -> {Log.w("observer 2", data.value)});
    }