Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.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
在处理RxJava concatMap之前提前调用onCompleted_Java_Android_Rx Java_Rx Android_Concatmap - Fatal编程技术网

在处理RxJava concatMap之前提前调用onCompleted

在处理RxJava concatMap之前提前调用onCompleted,java,android,rx-java,rx-android,concatmap,Java,Android,Rx Java,Rx Android,Concatmap,我有两个观测值,使用concatDelayError进行顺序处理 我的问题是onNext和onCompleted在处理之前很早就被调用了。 我如何知道所有处理都已使用concatDelayError完成 Psudo代码: public Observable<Integer> concat(){ int x = 10; int y = 20; Observable obx = Observable.create(emitter -> {

我有两个观测值,使用concatDelayError进行顺序处理

我的问题是onNext和onCompleted在处理之前很早就被调用了。 我如何知道所有处理都已使用concatDelayError完成

Psudo代码:

public Observable<Integer> concat(){
     int x = 10;
     int y = 20;


        Observable obx = Observable.create(emitter -> {
                    try {
                        int x = doSomeThing();
                        emitter.onNext(x);
                        emitter.onCompleted();
                    } catch (SQLiteException e) {
                        emitter.onError(e);
                    }
                }, Emitter.BackpressureMode.BUFFER);   
        Observable oby = Observable.create(emitter -> {
                    try {
                        int y = doSomeThing();
                        emitter.onNext(y);
                        emitter.onCompleted();
                    } catch (SQLiteException e) {
                        emitter.onError(e);
                    }
                }, Emitter.BackpressureMode.BUFFER);       
        Observable concated =  Observable.concatDelayError(ob1,ob2)
                 .compose(applySchedulers())
                 .replay().autoConnect();  

  }

    //somewhere else

    concat().subscribe(mReplaySubject);


    //somewhere else

     mReplaySubject.subscribe(new Observer<Integer>() {
                        @Override
                        public void onCompleted() {
                            launchActivity(SplashActivity.this, HomeActivity.class);
                            SplashActivity.this.finish();
                        }    
                        @Override
                        public void onError(Throwable e) {
                            e.printStackTrace();
                        }    
                        @Override
                        public void onNext(Integer value) {

                        }
                    });
我正在使用带有自动连接的reply和通过ReplySubject订阅,因为我需要共享单个订阅。

您可以在replay.autoConnect之前使用doOnSubscribe,但您还需要一种方法来确保返回的Observable最多设置一次:

final class OnceObservable {

    final AtomicReference<Observable<Integer>> ref = new AtomicReference<>();

    int x;
    int y;

    Observable<Integer> concat() {
        Observable<Integer> obs = ref.get();
        if (obs != null) {
            return;
        }

        obs = Observable.concatDelayError(
            Observable.fromCallable(() -> doSomething()),
            Observable.fromCallable(() -> doSomethingElse())
        )
        .doOnSubscribe(() -> {
            x = 10;
            y = 20;
        })
        .replay().autoConnect();

        if (!ref.compareAndSet(null, obs)) {
            obs = ref.get();
        }
        return obs;
    }
}

您确定您的Observer.onCompleted在一次订阅中被多次调用吗?请通过将.doOnEach应用于源观测值和连接的观测值来添加一些日志记录。否则它确实不清楚。@akarnokd我正在使用ReplaySubject。我已经改变了问题。你能回答这个问题吗?@akarnokd我登录了onCompleted,它没有多次呼叫,但在处理完成之前就已经提前呼叫了。你是否尝试过直接订阅concated而不使用ReplaySubject?此时,replay.autoConnect已为您执行缓存。最后一个问题:does.replay.autoConnect;自动缓存订阅?仍在调用onCompleted earlyWhat订阅?又如何检测早期未完成的?