Flowable不会调用onNext,onComplete of subscriber

时间:2017-03-24 03:40:08

标签: rx-java rx-android

我在Android应用中使用RxJava 2.0.7。我正在将代码从1.x迁移到2.x.我用Flowable替换了Observables,但在onNext中,onFmplete没有被触发。仅触发onSubscribe事件。我在这里错过了什么吗?

public Observable<ProfileMessage> getProfile(final String userId) {
    if (errorObservable != null)
        return errorObservable;

    return Flowable.create(new FlowableOnSubscribe<ProfileMessage>() {
        @Override
        public void subscribe(FlowableEmitter<ProfileMessage> e) throws Exception {
            e.onNext(getDummyProfile());
            e.onComplete();
        }
    }, BackpressureStrategy.BUFFER).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io());
}

private ProfileMessage getDummyProfile() {
    ProfileMessage profileMessage = new ProfileMessage();
    profileMessage.setAddress("Behind dattamandir, Alandi road, Dighi, Pune 411015");
    profileMessage.setAge(32);
    return profileMessage;
}

Observable<ProfileMessage> flowable = getProfile(LocalDataManager.getInstance().getUser().getUserId());
flowable.subscribe(new Observer<ProfileMessage>(){

    @Override
    public void onSubscribe(Subscription s) {
        Log.d("Swapnil","onSubscribe");
    }

    @Override
    public void onNext(ProfileMessage profileMessage) {
        Log.d("Swapnil","onNext"+Thread.currentThread().getName());
    }

    @Override
    public void onError(Throwable t) {
        Log.d("Swapnil","onError"+Thread.currentThread().getName());
    }

    @Override
    public void onComplete() {
        Log.d("Swapnil","onComplete"+Thread.currentThread().getName());       
    }
});

经过调查,发现FlowableCreate.java的条件不满足,因此不执行包含onNext的块。

long r = get();//has value 0
long e = 0L;
while (e != r) { //fails and onNext is not called

FlowableCreate.java有下面提到的排水方法。

void drain() {
    if (wip.getAndIncrement() != 0) {
        return;
    }

    int missed = 1;
    final Subscriber<? super T> a = actual;
    final SpscLinkedArrayQueue<T> q = queue;

    for (;;) {
        long r = get();
        long e = 0L;

        while (e != r) {
            if (isCancelled()) {
            q.clear();
            return;
        }

        boolean d = done;

        T o = q.poll();

        boolean empty = o == null;

        if (d && empty) {
            Throwable ex = error;
            if (ex != null) {
            super.onError(ex);
        } else {
            super.onComplete();
        }
        return;
        }

        if (empty) {
            break;
        }

        a.onNext(o);

        e++;
    }

    if (e == r) {
        if (isCancelled()) {
            q.clear();
            return;
        }

        boolean d = done;

        boolean empty = q.isEmpty();

        if (d && empty) {
            Throwable ex = error;
            if (ex != null) {
                super.onError(ex);
            } else {
                super.onComplete();
            }
            return;
        }
    }

    if (e != 0) {
        BackpressureHelper.produced(this, e);
    }

    missed = wip.addAndGet(-missed);
    if (missed == 0) {
        break;
    }
}
}

0 个答案:

没有答案