Android paging library + Firestore crashes with Il

2019-08-23 11:29发布

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条回答
Juvenile、少年°
2楼-- · 2019-08-23 12:08

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.

查看更多
登录 后发表回答