Firebase总是从互联网读取 [英] Firebase always reads from internet

查看:90
本文介绍了Firebase总是从互联网读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,Flutter上的Firebase会先自动从缓存中读取内容,但我在开发过程中注意到,一个应用程序只能处理近1个具有10或15个文档的流,而我读取的这些流超过4000次!通过这种方式,如果有10个用户使用了我的应用程序,我将付清我所拥有的全部费用,因此我已经重新检查了每个查询,快照并将此代码告知

As I know Firebase on Flutter will automatically read from cache first but I noticed while development that an app just play around with almost 1 stream that has 10 or 15 document I have reach reads above 4000 reads! By this way if 10 users has used my app I will pay all I have, so I have recheck every query, snapshot and put this code to know

print(itemDoc.metadata.isFromCache ? "itemDoc NOT FROM NETWORK" : "itemDoc FROM NETWORK");

编辑,编写代码,并详细说明

当我更新,添加,删除文档控制台打印所有存储的文档时,我注意到此问题出现在IOS中.

I noticed this issue appear in IOS when I update,add,delete a document console print all stored documents.

我拥有可以获取所有用户列表的Main Stream 然后我创建的每个列表流都将用于侦听列表中的项目

I have Main Stream that I get all users lists then every list I create stream for it to listen list's items

userListsStream(uid){
    Stream<QuerySnapshot> shoppingListsStream = 
    Firestore.instance.collection('lists').where('owner', arrayContains: uid).snapshots();

    shoppingListsStream.listen(
        (QuerySnapshot listsQuery) async {

          List<DocumentSnapshot> listsDocs = listsQuery.documents;
          if (listsDocs.length != 0) {
            //? foreach on lists Documents
            listsDocs.forEach(
              (DocumentSnapshot listDoc) async {
              print(listDoc.metadata.isFromCache ? "listDoc NOT FROM 
              NETWORK" : "listDoc FROM NETWORK");
              listItemsStream(listDoc.documentID);
         }
        )
       }
    })
}

listItemsStream(lid){
shoppingItemsRef =  Firestore.instance.collection('lists').document(lid).collection('items');

shoppingItemsRef.snapshots().listen(
        (QuerySnapshot itemsQuery) async {
          List<DocumentSnapshot> itemsDocs = itemsQuery.documents;
          if (itemsDocs.length != 0) {
            itemsDocs.forEach(
              (DocumentSnapshot itemDoc) async {
              print(itemDoc.metadata.isFromCache ? "itemDoc NOT FROM 
              NETWORK" : "itemDoc FROM NETWORK");
             }
           )
          }
}

我在Home.dart initState

 @override
  void initState() {
    Provider.of<ListsProvider>(context, listen: false)
        .userListsStream(uid);
  }

推荐答案

isFromCache标志指示文档是否保证与服务器保持最新,或者可能是缓存中的过期结果.如果是false,不一定表示已从服务器读取了文档,但是可以保证文档与服务器是最新的.

The isFromCache flag indicates whether the document is guaranteed to be up to date with the server, or that it may be a stale result from the cache. If it's false that doesn't necessarily means the document was read from the server, but that you can be guaranteed that the document is up to date with the server.

为了展示这意味着什么,我可以摘录

To show what this means, I can this snippet

var snapshotsStream = Firestore.instance.collection("chat").orderBy("timestamp", descending: true).limit(3).snapshots();
snapshotsStream.listen((querySnapshot) {
  print("We got a new QuerySnapshot");
  querySnapshot.documents.forEach((doc) {
    print(doc.documentID+": "+(doc.metadata.isFromCache ? "from CACHE " : "from SERVER "));
  });
  querySnapshot.documentChanges.forEach((docChange) {
    print(docChange.type.toString()+(docChange.document.metadata.isFromCache ? "doc from CACHE " : "doc from SERVER "));
  });
}, onError: (error) {
  print(error);
});

我最初得到的输出是:

flutter:我们获得了一个新的QuerySnapshot

flutter: We got a new QuerySnapshot

Flutter:5nAr5pYgwXJ0n3pWZkLw:来自服务器

flutter: 5nAr5pYgwXJ0n3pWZkLw: from SERVER

颤振:moysGY7Ea7TCf28fcEVC:来自服务器

flutter: moysGY7Ea7TCf28fcEVC: from SERVER

Flutter:PuNnPaiLMIE7704R9NuL:来自服务器

flutter: PuNnPaiLMIE7704R9NuL: from SERVER

颤振:5nAr5pYgwXJ0n3pWZkLw:来自SERVER的DocumentChangeType.addeddoc

flutter: 5nAr5pYgwXJ0n3pWZkLw: DocumentChangeType.addeddoc from SERVER

颤振:moysGY7Ea7TCf28fcEVC:来自SERVER的DocumentChangeType.addeddoc

flutter: moysGY7Ea7TCf28fcEVC: DocumentChangeType.addeddoc from SERVER

Flutter:PuNnPaiLMIE7704R9NuL:来自SERVER的DocumentChangeType.addeddoc

flutter: PuNnPaiLMIE7704R9NuL: DocumentChangeType.addeddoc from SERVER

然后,当我在服务器上进行更改时,它会打印:

Then when I make a change on the server, it prints:

flutter:我们获得了一个新的QuerySnapshot

flutter: We got a new QuerySnapshot

Flutter:5nAr5pYgwXJ0n3pWZkLw:来自服务器

flutter: 5nAr5pYgwXJ0n3pWZkLw: from SERVER

颤振:moysGY7Ea7TCf28fcEVC:来自服务器

flutter: moysGY7Ea7TCf28fcEVC: from SERVER

Flutter:PuNnPaiLMIE7704R9NuL:来自服务器

flutter: PuNnPaiLMIE7704R9NuL: from SERVER

Flutter:5nAr5pYgwXJ0n3pWZkLw:来自SERVER的DocumentChangeType.modifieddoc

flutter: 5nAr5pYgwXJ0n3pWZkLw: DocumentChangeType.modifieddoc from SERVER

因此,使用isFromCache属性并不是要确定该文档是否是服务器上的收费读物,而是要确定该文档是否可以与服务器保持最新.

So using the isFromCache property is not meant to determine whether the document was a charged read on the server, but whether the document is guaranteed to be up to date with the server.

要知道哪些文档已更改,可以遍历documentChanged集合,如上面的代码所示.

To know what documents have changed, you can iterate over the documentChanged collection, as shown in the code above.

关于读取的数据超出您的预期,最常见的原因之一是在Firebase控制台中保持Firestore面板处于打开状态.控制台执行的读取将计入您的项目.有关更多信息,请参见:

As to having more reads than you expected, one of the more common causes of this is keeping Firestore panel open in the Firebase console. Reads performed by the console are charged towards your project. For more on this, see:

  • Google Cloud Firestore console reading of all documents and charges
  • Firestore collection listeners enormous number of reads
  • Firestore - unexpected reads

这篇关于Firebase总是从互联网读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆