SwiftUI和Firebase-流错误:“未找到:没有要更新的文档:

时间:2020-08-01 09:12:56

标签: swift google-cloud-firestore swiftui

因此,我有一个程序,该程序在打开时会在特定集合(均已指定)中查找特定文档名称,并在找到该名称时复制该文档名称并启动侦听器。如果在5 x 5秒的间隔后找不到文档名称,该应用程序将停止。出于某种原因,当我运行代码时,在进行第一次检查之后,我得到了大约一千次错误写入:

[Firebase / Firestore] [I-FST000001] WriteStream(7ffcbec0eac8)流错误:“未找到:没有要更新的文档:

这是我用来调用Firestore的代码:

let capturedCode: String? = "party"

.onAppear(perform: {
            
            Timer.scheduledTimer(withTimeInterval: 5, repeats: true) { timer in
                print("running code check sequence")
                if let code = capturedCode {
                    calcCloud.checkSessionCode(code)
                    
                    if env.doesCodeExist {
                        print("code found! applying to environment!")
                        env.currentSessionCode = code
                        calcCloud.watchCloudDataAndUpdate()
                        allClear(env: env)
                        timer.invalidate()
                    }
                    else if timerCycles < 5 {
                        
                        timerCycles += 1
                        print("code not found, this is cycle \(timerCycles) of 5")
                        
                    } else {
                      print("could not find document on firebase, now committing suicide")
                        let x = ""
                        let _ = Int(x)!
                    }
                }
            }
        })

这是我用来检查Firebase的代码:


    func checkSessionCode(_ code: String) {
            print("checkSessionCode running")
                
                let docRef = self.env.db.collection(K.sessions).document(code)
                docRef.getDocument { (document, error) in
                    if document!.exists {
                        print("Document data: \(document!.data())")
                        self.env.doesCodeExist = true
                    } else {
                        print("Document does not exist")
                        self.env.doesCodeExist = false
    
                    }
                }
            }

以下是找到并应用该代码后应执行的代码:

func watchCloudDataAndUpdate() {
        
        env.db.collection(K.sessions).document(env.currentSessionCode!).addSnapshotListener { (documentSnapshot, error) in
            guard let document = documentSnapshot else {
                print("Error fetching snapshot: \(error!)")
                return
            }
            guard let data = document.data() else {
                print("Document data was empty.")
                return
            }

我哪里出错了,这个错误到底是什么...预先感谢:)

编辑:为清楚起见,似乎在onAppear完成执行后错误开始...

1 个答案:

答案 0 :(得分:1)

这就是为什么我需要在凌晨1点之后停止编码...在模拟器上,我删除了我的应用程序,然后重新启动,一切又开始正常工作了...有时最简单的答案是正确的...