【发布时间】:2020-04-07 15:59:42
【问题描述】:
我编写了一个函数来从 Firebase Firestore 获取特定文档。这是我的代码:
func getDocumentFromFirebase(collection: String, documentId: String) -> [String: String] {
var firebaseResult = [String: String]()
let docRef = db.collection(collection).document(documentId)
docRef.getDocument { (document, error) in
if let document = document, document.exists {
var property = document.get("name") as! String
//print("Document data string: \(property)")
firebaseResult["name"] = property
return firebaseResult
} else {
print("Document does not exist")
}
}
}
如您所见,我想在字典中返回结果,但返回行出现错误:
“void 函数中出现意外的非 void 返回值。”
但是当我将它向下移动几行时,在 Firebase 调用完成之前返回并且字典为空。
我知道我正在尝试在字典中返回结果,在 Void 函数中从 Firebase 获取文档。
我的问题是,检索到文档后如何在 Dictionary 中返回 Firebase 调用的结果?
谢谢!
【问题讨论】:
标签: ios swift firebase google-cloud-firestore