想标识自动生成的ID以在不同查询中引用。
{
docRef = db.collection(“用户”).document()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let document = docRef.documentID.self as? Int ?? {
let dataDescription = document.data().map(Int.init(describing:)) ?? "nil"
let profileName = dataDescription["firstname"] as? String ?? ""
self.nameLabel.text = profileName
print("Document data: \(profileName)")
} {
print("Document does not exist")
}
}
答案 0 :(得分:0)
在创建文档之前将文档引用声明为属性:
let userDocRef = Firestore.firestore().collection("users").document()
然后,当您在以后的查询中需要文档ID时,请从该属性中获取它:
let docID = userDocRef.documentID
这可能需要您将userDocRef
向前传递给执行查询的对象,或将属性公开给该对象。无论哪种方式,都取决于范围,如何传递或访问它取决于您的偏好。