RxJava:fromIterable不会发出所有项目

时间:2018-11-22 06:37:10

标签: android rx-java rx-java2

我正在尝试使用fromIterableflatMap运算符编写一个简单的代码。我只是在代码中将长值流映射到Result流。没什么

这是我的代码。 我的课空了

    class Result {
        @Override
        public String toString() {
            return "result";
        }
    }

还有一个功能

    public Observable<Result> getResults(List<Long> requests) {
        return Observable.fromIterable(requests)
                .flatMap( aLong -> {
                    Result[] items = {new Result(), new Result()};
                    return Observable.fromIterable(Arrays.asList(items));
                });
    }       

我对此表示赞同

     List<Long> ids = new ArrayList<>(Arrays.asList(1L, 2L, 3L));

     getResults(ids)
    .subscribe(new DisposableObserver<Result>() {
        @Override
        public void onNext(Result item) {
            Log.d(TAG, "onNext: " + item);
        }

        @Override
        public void onComplete() {
            Log.d(TAG, "onCompleted: ");
        }

        @Override
        public void onError(Throwable e) {
            Log.e(TAG, "onError: " + e.getMessage());
        }
    });     

我的预期输出是

    LOG: onNext: result
    LOG: onNext: result
    LOG: onNext: result
    LOG: onNext: result
    LOG: onNext: result
    LOG: onNext: result
    LOG: onCompleted:         

对于fromIterable的每次发射,flatMap预期将返回具有2个值的流,因此总计6次onNext,然后是onComplete

但是我得到的是

    LOG: onNext: result
    LOG: onNext: result
    LOG: onCompleted:        

只有2次onNext触发,然后完成。其余4个值去哪了?

但是奇怪的是,我在Log函数的doOnNext的{​​{1}}的{​​{1}}中添加了fromIterable

getResults

现在它正在发射所有值!!!!这是输出

    public Observable<Result> getResults(List<Long> requests) {
        return Observable.fromIterable(requests)
                .doOnNext(i -> Log.d(TAG, "fromIterable emitted " + i))
                .flatMap(aLong -> {
                    Result[] items = {new Result(), new Result()};
                    return Observable.fromIterable(Arrays.asList(items));
                });
    }

这是怎么回事? LOG: fromIterable emitted 1 LOG: onNext: result LOG: onNext: result LOG: fromIterable emitted 2 LOG: onNext: result LOG: onNext: result LOG: fromIterable emitted 3 LOG: onNext: result LOG: onNext: result LOG: onCompleted: 对链进行了哪些更改以发出所有值?

1 个答案:

答案 0 :(得分:0)

如@akarnokd所解释,您正在记录非常短的相似文本。日志在您的情况下应该是唯一的

public class Main extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainlayout);
    }
    public void clicked(View v){
        switch (v.getId()){
            case R.id.a:
                setContentView(R.layout.alayout);
                break;
            case R.id.b:
                setContentView(R.layout.blayout);
                break;
        }
    }

}

或类似地在您的观察中提及

List<Long> ids = new ArrayList<>(Arrays.asList(1L, 2L, 3L));

    getResults(ids)
            .subscribe(new DisposableObserver<Result>() {
                @Override
                public void onNext(Object item) {
                    Log.d(TAG, "onNext: called at " + System.currentTimeMillis());
                }

                @Override
                public void onComplete() {
                    Log.d(TAG, "onCompleted: ");
                }

                @Override
                public void onError(Throwable e) {
                    Log.e(TAG, "onError: " + e.getMessage());
                }
            });