Android分页库+ Firestore因IllegalStateException崩溃 [英] Android paging library + Firestore crashes with IllegalStateException

查看:128
本文介绍了Android分页库+ Firestore因IllegalStateException崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Firestore作为后端来实现新的Android分页库。我创建了一个扩展 PageKeyedDataSource< Integer,MyObject> 的类 MyDataSource ,在其中实现了三个功能: / p>


  • loadInitial

  • loadBefore

  • loadAfter



例如功能之一是:

  @Override 
public void loadInitial(@NonNull LoadInitialParams< Integer>参数,@NonNull最终LoadInitialCallback< Integer,MyObject>回调){
query.addSnapshotListener((snapshots,exception)-> {
if(exception!= null)return;

List< MyObject> list = new ArrayList<>();
for(DocumentSnapshot document:snapshots.getDocuments()){
list.add(document.toObject(MyObject.class));
}
callback.onResult(list,null,1); //错误
});
}

一切正常,直到数据库中的某些内容发生更改,调用了侦听器,并且应用程序崩溃,原因如下:


java.lang.IllegalStateException:callback.onResult已被调用,无法再次调用。


我尝试使用 get()正常运行。我需要获取实时更新。



如何避免此错误?

解决方案

不幸的是,您不能同时进行实时更新和分页。您必须选择一个。这是分页库的限制,它需要在内部管理结果页面。


I trying to implement the new Android paging library using Firestore as my backend. I have created a class MyDataSource that extends PageKeyedDataSource<Integer, MyObject>, where I'm implementing three functions:

  • loadInitial
  • loadBefore
  • loadAfter

For example one of the functions is this:

@Override
public void loadInitial(@NonNull LoadInitialParams<Integer> params, @NonNull final LoadInitialCallback<Integer, MyObject> callback) {
    query.addSnapshotListener((snapshots, exception) -> {
        if (exception != null) return;

        List<MyObject> list = new ArrayList<>();
        for(DocumentSnapshot document : snapshots.getDocuments()){
            list.add(document.toObject(MyObject.class));
        }
        callback.onResult(list, null, 1); //Error
    });
}

Everythings works fine until something in the database changes, the listener is called and the app crashes with:

java.lang.IllegalStateException: callback.onResult already called, cannot call again.

I tried using get() and it worked fine. My requirements to get realtime updates.

How to avoid this error?

解决方案

Unfortunately, you can't have both realtime updates and paging at the same time. You have to choose one or the other. This is a limitation of the paging library, which needs to manage pages of results internally.

这篇关于Android分页库+ Firestore因IllegalStateException崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆