FieJoeKot
FieJoeKot

Reputation: 11

Improve Firestore Offline cache - Android

So half of my application relies quite a bit on Firestore.

Sometimes, it takes quite a long time, like 5000ms or more to load my documents. If it was images or something else maybe I'd understand but it's mainly strings or Ints...

Any ideas on how I could improve this?

Thanks


EDIT: db.collection("usersAuth/${FirebaseAuth.getInstance().uid!!}/KitLists").get().addOnSuccessListener { snapshot ->

        for (document in snapshot.documents) {
            val data = document

            val kitName = data.id
            firstKitList.add(kitName)
        }

       mainListViewAdapter.notifyDataSetChanged()
    }

EDIT2

So, I adapted it, but I have an unresolved error on snapshot.

db.collection("usersAuth/${FirebaseAuth.getInstance().uid!!}/KitLists").addSnapshotListener(object : EventListener<QuerySnapshot> {
        override fun onEvent(@Nullable value: QuerySnapshot, @Nullable e: FirebaseFirestoreException?) {
            if (e != null) {
                Log.w("TAG", "Listen failed.", e)
                return
            }

            for (document in snapshot.documents) {
            val data = document

            val kitName = data.id

            firstKitList.add(kitName)

        }

       mainListViewAdapter.notifyDataSetChanged()
        }
    })

this is the error snapshot error

Upvotes: 0

Views: 1804

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138824

If you are using a get() call, you need to know that you are trying to read data over the internet. You cannot compare this operation with an operation of reading an SQLite database, which is stored locally on the disk. The speed of getting the data from Firebase servers depends on the speed of your internet connection and on the amount of data that you are trying to get. So most likely the reason for waiting 5000ms is one of these, or why not both. If the reason is the amount of data, try to optimize your queries or try to get the data in small parts.

If we are speaking about the first attempt to read a document, it might be slower than the subsequent ones, because it has to initiate the internet connection. I know that the Firebase team is trying to improve the performance, but you can't expect 0ms when retrieving data over a network.

One thing that you can do, is to enable offline persistence which will create a local cache for the data that was previously read. But a get() request will first check Firebase servers. If you use an addSnapshotListener(), you'll be able to read the data from the cache instantly, without checking the network.

If you want to listen to a single document, please use the following code:

yourDocumentReference.addSnapshotListener(new EventListener<DocumentSnapshot>() {
    @Override
    public void onEvent(@Nullable DocumentSnapshot snapshot, @Nullable FirebaseFirestoreException e) {
        if (e != null) {
            Log.w(TAG, "Listen failed.", e);
            return;
        }

        if (snapshot != null && snapshot.exists()) {
            Log.d(TAG, "Current data: " + snapshot.getData());
        } else {
            Log.d(TAG, "Current data: null");
        }
    }
});

If you want to listen to multiple documents in a collection, please use the following code:

yourCollectionReference.addSnapshotListener(new EventListener<QuerySnapshot>() {
    @Override
    public void onEvent(@Nullable QuerySnapshot value, @Nullable FirebaseFirestoreException e) {
        if (e != null) {
            Log.w(TAG, "Listen failed.", e);
            return;
        }

        List<String> cities = new ArrayList<>();
        for (DocumentSnapshot doc : value) {
            if (doc.get("name") != null) {
                cities.add(doc.getString("name"));
            }
        }
        Log.d(TAG, "Current cites in CA: " + cities);
    }
});

Upvotes: 5

Related Questions