现在我在聊天片段中使用Google分页库
这里是我的数据源中Initial的代码:
Disposable disposable = apiWrapper.getMessages(1, userId)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(messagesPagingResponse -> {
if (messagesPagingResponse.getData() != null && messagesPagingResponse.getData().getData() != null) {
callback.onResult(messagesPagingResponse.getData().getData(), null, messagesPagingResponse.getData().getNextPage());
}
}
, throwable -> {
Log.e("throwable", throwable.getLocalizedMessage());
});
compositeDisposable.add(disposable);
并且在聊天片段中,我观察到该列表
viewModel.getLiveMessages().observe(this, this::setChatList);
private void setChatList(PagedList<Message> messages) {
this.messages = messages;
ChatPagingAdapter chatPagingAdapter = (ChatPagingAdapter) binding.messagesRecyclerView.getAdapter();
if (chatPagingAdapter != null){
chatPagingAdapter.submitList(this.messages);
}
}
它运行良好,直到iam尝试向分页列表添加新消息,以便它向我显示此错误
E/error: The exception could not be delivered to the consumer because it has already canceled/disposed the flow or the exception has nowhere to go to begin with. Further reading: https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0#error-handling | java.lang.UnsupportedOperationException
当我收到新消息时,我尝试以此添加它
messages.add(0, message);
[在与您类似的情况下,我使用Room保留了所有聊天消息,并使用PagedList
(数据访问对象)中的LiveData
构建了DataSource.Factory
Dao
。每当有新消息时,您要做的就是保留该消息,然后会议室将更新发送到您的PagedList
Livedata
,并相应地对Chats RecyclerView进行更新。
如果您不熟悉Room,可以从offical documentation中阅读更多内容>