0

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?

Oleg Caralanski
  • 330
  • 2
  • 10
  • I don't think it's possible to get realtime updates at the same time as paging. As the error message says, onResult can't be called a second time, which is precisely what's happening when you add a listener that gets invoked with each change to the query. – Doug Stevenson Feb 13 '19 at 13:48
  • @DougStevenson Thanks for answering. So you basically say that Firestore cannot work with the new Android paging library in realtime, right? When only paging data without the realtime feature everything works perfect. – Oleg Caralanski Feb 13 '19 at 13:56
  • Firestore works fine with paging. You just can't get realtime updates at the same time as using the paging library. This is a limitation of the paging library. – Doug Stevenson Feb 13 '19 at 14:46
  • @DougStevenson Thanks you so much for the clarification. That's the answer that i was looking for. Please add it as an answer so I can accept it. Literally saved my day. – Oleg Caralanski Feb 13 '19 at 14:51

1 Answers1

-3

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.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Thanks again so much!! – Oleg Caralanski Feb 13 '19 at 14:58
  • Firebase has so many limitations. "Realtime updates and paging not available at same time...." Its very necessary feature. Although lots of other basic and necessary features are also not available in Firebase. – Pooja Sep 19 '20 at 21:03
  • @Pooja If you have feedback for the Firebase team, send that to Firebase support directly. This is not something Stack Overflow can address, and downvoting this answer does not help. https://support.google.com/firebase/contact/support – Doug Stevenson Sep 19 '20 at 21:09