0

I'm using Firebase Cloud Firestore with Firestore UI RecyclerView to display items on MainActivity. Everything works fine except that, when I uninstall and re-install the app, query does not fetch previously added items and only an empty list appears. When I add a new item to Firestore after re-install, only that item is fetched and still no previous data. However, I can see both previously added data and the new item on Firebase Console.

Has anyone experienced a similar issue or any idea what can cause this?

My function that sets up the RecyclerView is as follows. I call this function and then call adapter.startListening() in onStart and adapter.stopListening() in onStop.

private void setupRecyclerView() {
    if(shouldStartSignIn()) return;
    if(!PermissionUtils.requestPermission(this, RC_STORAGE_PERMISSION, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
        Log.e(TAG, "Permission not granted, don't continue");
        return;
    }
    LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    pagesRecyclerView.setLayoutManager(layoutManager);
    Query query = FirebaseFirestore.getInstance().collection(Page.COLLECTION).whereEqualTo(Page.FIELD_USER_ID, FirebaseAuth.getInstance().getCurrentUser().getUid()).orderBy(Page.FIELD_TIMESTAMP).limit(50);
    FirestoreRecyclerOptions<Page> options = new FirestoreRecyclerOptions.Builder<Page>().setQuery(query, Page.class).build();
    adapter = new PagesAdapter(options, this);

    pagesRecyclerView.setAdapter(adapter);
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
monatis
  • 534
  • 4
  • 8

1 Answers1

0

You have initialized yourquery but the query is not attached to any listener which actually pulls the data. So your adapter is empty. Basically you need to do something like this:

query.addSnapshotListener(new EventListener<QuerySnapshot>() {
  @Override
  public void onEvent(@Nullable QuerySnapshot snapshot,
                    @Nullable FirebaseFirestoreException e) {
      if (e != null) {
          // Handle error
          //...
          return;
      }

      // Convert query snapshot to a list of chats
      List<Chat> chats = snapshot.toObjects(Chat.class);

      // Update UI
      // ...
  }
});

You can read more about it here: https://github.com/firebase/FirebaseUI-Android/tree/master/firestore#querying

Parth Bhoiwala
  • 1,282
  • 3
  • 19
  • 44
  • I'm using [FirebaseUI library](https://github.com/firebase/FirebaseUI-Android/tree/master/firestore), which provides `FirestoreRecyclerAdapter` with a built-in listener. So, I just need to call `adapter.startListening()` to trigger that listener according to `readme.md` page of that repository. – monatis Apr 13 '18 at 14:46
  • I have updated the code snippet and the link, hope that helps – Parth Bhoiwala Apr 13 '18 at 15:24