[英]How to get Full Document from firestore including the reference Data populated
I am using Firestore in my Swift project.我在我的 Swift 项目中使用 Firestore。 I would like to get all the reference data inside the document populated when I fetch the list.我想在获取列表时获取填充文档中的所有参考数据。 But currently, I only get the reference Ids.但目前,我只得到参考 ID。
Here is the sample code that I am using to get the list of conversations这是我用来获取对话列表的示例代码
func getConversationByUserId(createdBy: String, completion: @escaping ([Conversation])->()) {
let conversationRef = DBHelper.shared.database.collection("conversations")
let createdByRef = DBHelper.shared.database.document("users/\(createdBy)")
conversationRef
.whereField("members", arrayContains: createdByRef)
.addSnapshotListener() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
if querySnapshot!.documents.isEmpty {
completion([])
} else {
var conversations: [Conversation] = []
if let snaps = querySnapshot?.documents {
for item in snaps {
conversations.append(Conversation(snapshot: item))
}
}
completion(conversations)
}
}
}
}
Firestore is a NoSQL database, we need to switch our coding mind from SQL database. Firestore 是一个 NoSQL 数据库,我们需要从 SQL 数据库切换我们的编码思路。
I would recommend to either store all related information in one document or you can fetch all needed information with multiple calls.我建议要么将所有相关信息存储在一个文档中,要么您可以通过多次调用获取所有需要的信息。
If you want to simplify the front-end logic, you can consider use Firebase Cloud Function to take over the multiple fetching logic.如果想简化前端逻辑,可以考虑使用 Firebase Cloud Function 来接管多次抓取逻辑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.