Why is OnComplete not called in this code? (RxAndroid)

帅比萌擦擦* 提交于 2019-12-11 13:54:32

问题


I know OnComplete of a Observer is called when all the items are emitted. In the below code, I am putting data from a cursor to an ArrayList in a flatMap operator. My cursor has 100 entries ( c.getCount() gives 100 ) and the size of my list is 100. The onNext is called 100 times too. But the onComplete is not called. I am populating a listview in onComplete.

static int i = 0;
final List<String> ar = new ArrayList<>();
ListView lv = ...;
ArrayAdapter<String> adapter = ...;   
.
.
. 
q.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                    .flatMap(new Func1<SqlBrite.Query, Observable<String>>() {
                @Override
                public Observable<String> call(SqlBrite.Query query) {
                    Cursor c = query.run();
                    c.moveToFirst();
                    Log.d("testApp", String.valueOf(c.getCount())); // prints 100
                    do {
                        ar.add(c.getString(0));
                    } while (c.moveToNext());
                    Log.d("testApp", String.valueOf(ar.size())); // prints 100
                    return Observable.from(ar);
                }
            }).subscribe(new Observer<String>() {
                @Override
                public void onCompleted() {
                    Log.d("testApp", "onComplete called");
                    lv.setAdapter(adapter);
                }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onNext(String s) {
                Log.d("testApp", "onNext called " + String .valueOf(++i) + " with " + s); // printed 100 times
                adapter.add(s);
            }
        });

The new stream of Observable is to complete after 100 but it does not.

Any help is appreciated, Thank You


回答1:


FlatMap operator merge your main stream with your flatMapped stream. Your observable will complete if q and your Observable from your array complete. So check why q doesn't no complete.



来源:https://stackoverflow.com/questions/31316694/why-is-oncomplete-not-called-in-this-code-rxandroid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!