Android paging library + Firestore crashes with IllegalStateException

折月煮酒 提交于 2019-12-13 04:01:19

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/54670606/android-paging-library-firestore-crashes-with-illegalstateexception

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