簡體   English   中英

Swift DispatchGroup導致錯誤:[Firebase / Firestore] [I-FST000001]無法到達Cloud Firestore后端

[英]Swift DispatchGroup causes error: [Firebase/Firestore][I-FST000001] Could not reach Cloud Firestore backend

我正在使用Firebase和Firestore通過以下方式讀取數據:

var itemData: [String:Any]?
let docRef = db.collection("store").document(documentID)

let group = DispatchGroup()
group.enter()
DispatchQueue.global().async {
    docRef.getDocument { (document, error) in
        if let document = document, document.exists {
            print("Document data: \(document.data() ?? ["key":"__data__"])")
            itemData = document.data()
        } else {
            print("Document does not exist")
        }
        group.leave()
    }
}
group.wait()
print("ITEM_DATA: \(itemData)")
return itemData

當我刪除任何DispatchGroup,group.enter()等的所有引用時,代碼可以正常運行,盡管不是我想要的順序(即,它運行print("ITEM_DATA: \\(itemData)")行,其中itemData為nil,直到以后才打印出print("Document data: \\(document.data() ?? ["key":"__data__"])") )的結果。 因此,我知道對Firebase的實際請求沒有任何問題。

但是,當我嘗試在寫入之前,使用上面的代碼中的DispatchGroup來解決打印/返回itemData的問題時,當我運行該函數時,程序會給我錯誤代碼:

[Firebase/Firestore][I-FST000001] Could not reach Cloud Firestore backend. Backend didn't respond within 10.000000 seconds.
This typically indicates that your device does not have a healthy Internet connection at the moment. The client will operate in offline mode until it is able to successfully connect to the backend.

收到此消息后,該程序再也不會連接到Firestore。 我已經在有和沒有wifi的模擬器和實際設備上進行了嘗試,但是沒有用。 我知道這不是我的Internet連接問題,因為當我用DispatchGroup刪除所有內容時它可以工作,但是我仍然無法弄清楚問題是什么或如何解決此問題。

嘗試進入和離開調度組在同一Queue

let group = DispatchGroup()
DispatchQueue.global().async {
    group.enter()
    docRef.getDocument { (document, error) in
        if let document = document, document.exists {
            print("Document data: \(document.data() ?? ["key":"__data__"])")
            itemData = document.data()
        } else {
            print("Document does not exist")
        }
        group.leave()
    }
}
group.wait()
print("ITEM_DATA: \(itemData)")

如果您想知道我的方法有何變化?

看看在DispatchQueue異步關閉內部移動的enter語句

是什么原因引起的?

進入一個在不同隊列中的調度組,而在另一個隊列中離開調度組,可能是導致此問題的原因

編輯1:

嘿,剛剛測試了代碼並使其正常工作

let group = DispatchGroup()
    DispatchQueue.global().async {
        group.enter()
        sleep(10)
        debugPrint("Hello")
        group.leave()
    }
    group.notify(queue: DispatchQueue.main) {
        print("ITEM_DATA: )")
    }

這是控制台的輸出順序

  1. “你好”
  2. ITEM_DATA :)

我知道我沒有使用wait()並且wait()應該也可以工作,但是我的工作有點忙,所以現在無法使用wait進行測試

編輯2:

剛剛意識到OP打算使用Dispatch group從異步函數返回值,因此更新了我的答案

無論您采用哪種邏輯,都不能從異步調用獲得return語句。 派遣小組在這里無濟於事,您所需要的只是closureblock

func test(onCompletion completion: @escaping ([String:Any]) -> ()) {
    DispatchQueue.global().async {
        docRef.getDocument { (document, error) in
            if let document = document, document.exists {
                completion(document.data())
            }
        }
    }
}

並稱之為

    self.test {[weak self] (data) in
        debugPrint(data)
    } 

希望能幫助到你

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM