本文介绍了改善Firestore离线缓存-Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的应用程序中有一半非常依赖Firestore.

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

有时,它需要相当长的时间来加载我的文档,例如5000ms或更多.如果是图片或其他内容,我可能会理解,但主要是字符串或整数...

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?

谢谢

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

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


EDIT2

因此,我对其进行了修改,但是在snapshot上有一个未解决的错误.

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()
        }
    })

这是错误

推荐答案

如果使用的是get()请求,则需要知道您正在尝试通过Internet读取数据.您不能将其与尝试读取SQLite数据库的尝试进行比较,该数据库存储在磁盘上.从Firebase服务器获取数据的速度取决于您的Internet连接速度以及您要获取的数据量.因此,等待5000ms的原因很可能是其中之一.如果原因是数据量很大,请尝试优化查询或尝试将数据分成小部分.

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.

如果我们是第一个自动阅读文档,那么它可能会比随后的文档要慢,因为它必须启动Internet连接.我知道Firebase团队正在努力提高性能,但是当通过网络检索数据时,您不会指望0ms.

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.

您可以做的一件事就是启用offline persistence,这将为以前读取的数据创建本地缓存.但是get()请求将首先检查Firebase服务器.如果使用addSnapshotListener(),则可以立即从缓存中读取数据,而无需检查网络.

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);
    }
});

这篇关于改善Firestore离线缓存-Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 12:34