Improve Firestore Offline cache - Android

被刻印的时光 ゝ 提交于 2019-12-18 09:40:02

问题


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


回答1:


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

If we are speaking abot the first atempt to read a document, it might be slower than the subsequent ones, because it has to initiate the internet connection. I know that 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 to enable offline persistence which will create a local chache for the data that was previously read. But a get() request will first check Firebase servers. If you use a 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 folowing 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);
    }
});


来源:https://stackoverflow.com/questions/48807159/improve-firestore-offline-cache-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!