0

I want to reduce reading from FireStore. if, when a new data is added, it should only fetch that data. I don't want it to read the whole database again. I think this would be unnecessary and costly. I tried the code below but it doesn't work, it doesn't pull any data

https://medium.com/firebase-tips-tricks/how-to-drastically-reduce-the-number-of-reads-when-no-documents-are-changed-in-firestore-8760e2f25e9e

    query.get(CACHE).addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            if (task.isSuccessful()) {
                boolean isEmpty = task.getResult().isEmpty();
                if (isEmpty) {
                    query.get(SERVER).addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                        @Override
                        public void onComplete(@NonNull Task<QuerySnapshot> task) {
                            for (QueryDocumentSnapshot documentSnapshot : task.getResult()) {
                                Item item= documentSnapshot.toObject(Item.class);
                                items.add(item);
                                adapter.notifyDataSetChanged();
                            }

                        }
                    });
                }
                }
        }
    });

And when i try other method, reads data in memory when offline but reloads all database when online. thank you in advance for any of suggestions.

                query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                         for (QueryDocumentSnapshot documentSnapshot : task.getResult()) {
                                Item blogPost = doc.getDocument().toObject(Item.class);
                                items.add(blogPost);
                                mAdapter.notifyDataSetChanged();
                         }
                        
                    }
                });
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

1 Answers1

1

When you are using a get() call, you are always getting the online version of the documents from the Firebase servers. If you only want to get what's new, you should consider tracking the changes between snapshots. In this way, you can get only the data the corresponds to a specific operation.

But remember that there is no way you can skip the initial data. I have answered a question regarding this topic too:

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Snapshotlistener not working as i want. Reloads the all database when the disconnected or the application is exited. I tried to do as in Your article. can you show code sample? Thank you for Your answer though – Cavid Cavidd Oct 03 '21 at 12:20
  • What exactly have you tried from that article and doesn't work the way you expect? – Alex Mamo Oct 04 '21 at 07:48