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?