rxJava Observer.onNext不被第二次调用 [英] rxJava Observer.onNext not called second time
问题描述
我正在使用rxJava从数据库中获取数据并将其显示在recyclerview中.相关代码如下所示
I am using rxJava to fetch data from the database and show it in a recyclerview. The relevant code is shown below
function updateUI(){
ContactsLab contactsLab = ContactsLab.get(getActivity());
Subscription sub = contactsLab.getContactList().subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.toList()
.subscribe(onContactsReceived());
mCompositeSubscription.add(sub);
}
ContactsLab
是一个单例,它返回一个Observable of Contact对象. onContactsReceived
函数如下所示
ContactsLab
is a singleton that returns an Observable of Contact objects.
onContactsReceived
function is shown below
private Observer<List<Contact>> onContactsReceived(){
return new Observer<List<Contact>>() {
@Override
public void onCompleted() {}
@Override
public void onError(Throwable e) {}
@Override
public void onNext(List<Contact> contacts) {
if(mContactsAdapter == null) {
mContactsAdapter = new ContactsAdapter(contacts);
mRecyclerView.setAdapter(mContactsAdapter);
} else{
mContactsAdapter.setContactList(contacts);
mContactsAdapter.notifyDataSetChanged();
}
}
};
}
在我的片段 onResume
中调用了 updateUI
函数,但是视图仅在第一次更新.如果我从其他任何片段(已经向数据库添加了更多项目)回到这个片段,则调用 onResume
,运行 updateUI
,同时也调用 onContactsReceived
运行,但立即返回而无需调用 onNext
或 onComplete
.
The updateUI
function is called in my fragment onResume
but the view is updated only the first time. If I come back to this fragment from any other fragment (having added more items to db), onResume
is called, updateUI
runs and onContactsReceived
also runs but returns immediately without calling onNext
or onComplete
.
我认为这与rxJava处理可观察对象的方式有关,但不知道如何解决(了解 defer
但了解得不多).有人可以帮忙吗?
I think this has something to do with the way rxJava handles observables but no idea how to fix it (read about defer
but couldn't understand much). Can somebody please help?
getContactList
函数如下所示:
public rx.Observable<Contact> getContactList() {
List<Contact> contacts = new ArrayList<>();
ContactCursorWrapper cursorWrapper = queryContacts(null, null);
try{
cursorWrapper.moveToFirst();
while (!cursorWrapper.isAfterLast()){
contacts.add(cursorWrapper.getContact());
cursorWrapper.moveToNext();
}
} finally {
cursorWrapper.close();
}
return rx.Observable.from(contacts);
}
基本上,它查询数据库并将返回的 Cursor
映射到我的Contact类(这是一个POJO)中.我添加了 rx.Observable.from
,以获得一个可观察的对象,该对象随后使用 toList
进行了整理,并更新到了适配器中.我使用这种方法避免了在获取每个项目后必须调用 notifyDataSetChanged
(并且在获取所有内容后仅调用一次).
Basically it queries the database and maps the returned Cursor
into my Contact class(which is a POJO). I added the rx.Observable.from
to get an observable that was later collated using toList
and updated into the adapter.
I used this approach avoid having to call notifyDataSetChanged
after getting each item (and call it only once after getting all that).
什么是最小化 notifyDataSetChanged
调用次数以及每次调用 onResume
时刷新的正确方法?
What's the right approach to minimize the number of notifyDataSetChanged
calls and also, refresh each time onResume
is called?
推荐答案
您可观察到的 contactsLab.getContactList().toList()
已终止. toList()
收集所有从源可观察到的发射到列表,并在源可观察到终止后发射整个列表(请参见文档).您将不会再观察到它的排放.
Your observable contactsLab.getContactList().toList()
has terminated.toList()
collects all emissions from a source observable to a list and emits the entire list once the source Observable terminates (see the documentation). You aren't going to observe any more emissions from it.
这篇关于rxJava Observer.onNext不被第二次调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!