rxJava Observer.onNext 没有被第二次调用
rxJava Observer.onNext not called second time
我正在使用 rxJava 从数据库中获取数据并将其显示在 recyclerview 中。相关代码如下
function updateUI(){
ContactsLab contactsLab = ContactsLab.get(getActivity());
Subscription sub = contactsLab.getContactList().subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.toList()
.subscribe(onContactsReceived());
mCompositeSubscription.add(sub);
}
ContactsLab
是一个单例,returns 一个 Observable of Contact 对象。
onContactsReceived
函数如下图
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
也运行但 returns 立即没有调用 onNext
或 onComplete
.
我认为这与 rxJava 处理 observables 的方式有关,但不知道如何修复它(阅读 defer
但不太了解)。有人可以帮忙吗?
编辑:
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
映射到我的联系人 class(这是一个 POJO)。我添加了 rx.Observable.from
以获得一个可观察对象,该对象后来使用 toList
进行整理并更新到适配器中。
我使用这种方法避免在获取每个项目后调用 notifyDataSetChanged
(并且在获取所有项目后只调用一次)。
减少 notifyDataSetChanged
调用次数以及每次调用 onResume
时刷新的正确方法是什么?
您的 observable contactsLab.getContactList().toList()
已终止。toList()
收集来自源 observable 的所有发射到列表,并在源 Observable 终止后发射整个列表(参见 documentation)。您不会再观察到它的任何排放。
我正在使用 rxJava 从数据库中获取数据并将其显示在 recyclerview 中。相关代码如下
function updateUI(){
ContactsLab contactsLab = ContactsLab.get(getActivity());
Subscription sub = contactsLab.getContactList().subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.toList()
.subscribe(onContactsReceived());
mCompositeSubscription.add(sub);
}
ContactsLab
是一个单例,returns 一个 Observable of Contact 对象。
onContactsReceived
函数如下图
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
也运行但 returns 立即没有调用 onNext
或 onComplete
.
我认为这与 rxJava 处理 observables 的方式有关,但不知道如何修复它(阅读 defer
但不太了解)。有人可以帮忙吗?
编辑:
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
映射到我的联系人 class(这是一个 POJO)。我添加了 rx.Observable.from
以获得一个可观察对象,该对象后来使用 toList
进行整理并更新到适配器中。
我使用这种方法避免在获取每个项目后调用 notifyDataSetChanged
(并且在获取所有项目后只调用一次)。
减少 notifyDataSetChanged
调用次数以及每次调用 onResume
时刷新的正确方法是什么?
您的 observable contactsLab.getContactList().toList()
已终止。toList()
收集来自源 observable 的所有发射到列表,并在源 Observable 终止后发射整个列表(参见 documentation)。您不会再观察到它的任何排放。