【问题标题】:Firebase Firestore Get Document not working with SwiftFirebase Firestore 获取文档不适用于 Swift
【发布时间】:2017-10-31 16:14:01
【问题描述】:

我想尝试使用 Firebase Cloud Firestore 使用以下代码获取一些数据。

按下按钮应该获取一个布尔值,如果它满足某些条件,应用程序将导航到另一个视图控制器,它在第一次单击时工作正常,但我注意到当我导航返回并按下同一个按钮时,不会有任何结果又来了!!

db.collection("users").document((Auth.auth().currentUser?.uid)!).getDocument { (snap, error) in
                if error != nil {
                    print(error.debugDescription)
                }
                if snap != nil {
                    let isPosted = snap?.data()["isPosted"] as! Bool
                    let isStroe = snap?.data()["isStore"] as! Bool
                    if !isPosted && !isStroe {
                        print("first post")
                        let useraccountVC = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "newData") as! UINavigationController
                        self.present(useraccountVC, animated: true, completion: nil)
                    }else if isPosted && isStroe {
                        print("store")
                        let useraccountVC = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "newData") as! UINavigationController
                        self.present(useraccountVC, animated: true, completion: nil)
                    }else if !isPosted && isStroe{
                        print("store")
                        let useraccountVC = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "newData") as! UINavigationController
                        self.present(useraccountVC, animated: true, completion: nil)
                    }else{

                        print("cant post any more")
                    }

                }

            }

【问题讨论】:

    标签: swift google-cloud-firestore


    【解决方案1】:

    这个定价模型makes an explicit provision

    此外,如果侦听器断开连接的时间超过 30 分钟(例如,如果用户下线),您将被收取读取费用,就像您发出了全新的查询一样。

    这也适用于获取:如果您在 30 分钟内重复获取并且没有任何更改,则不收取任何费用。

    其工作方式是 Firestore 包含一个“恢复令牌”,其中包含自上次发出任何给定查询以来请求更改的查询。

    您可以通过enabling debug logging 验证客户端是否正在为您自己发送简历令牌。当您多次进行查询时,您会在日志中看到第一次以外的查询如下所示:

    Nov  1 16:44:36  Firestore_Example[8105] <Debug>: [Firebase/Firestore][I-FST000001] FSTWatchStream 0x6080002cbc20 watch: <GCFSListenRequest 0x6180002cc010>: {
      database: "projects/my-project/databases/(default)"
      add_target {
        documents {
          documents: "projects/my-project/databases/(default)/documents/test-collection/c1GK3eq7rakmRvIN4ehE"
        }
        resume_token: "\n\t\010\254\251\304\242\307\236\327\002"
        target_id: 2
      }
    }
    

    如果在您重复查询时服务器上没有任何更改,您还可以观察到,尽管发出了第二个请求,但响应实际上不会传输任何文档,它们只会表明客户端是最新的。

    【讨论】:

    • 好的,但我的问题是我需要该布尔值来执行某些功能!即使数据在服务器端没有改变
    • 总是返回数据。如果您的文档没有更改,并且您在 30 分钟内重复请求,则无需为验证客户端是否为最新的查询付费。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-04
    • 1970-01-01
    • 2022-01-06
    • 2021-06-03
    • 2021-09-25
    • 2021-07-22
    • 1970-01-01
    相关资源
    最近更新 更多