如何从Firestore检索自动生成的documentID,以重用documentID,以便不替换我的数据

时间:2020-08-09 09:08:45

标签: ios swift google-cloud-firestore

想标识自动生成的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")
        }
    }

1 个答案:

答案 0 :(得分:0)

在创建文档之前将文档引用声明为属性:

let userDocRef = Firestore.firestore().collection("users").document()

然后,当您在以后的查询中需要文档ID时,请从该属性中获取它:

let docID = userDocRef.documentID

这可能需要您将userDocRef向前传递给执行查询的对象,或将属性公开给该对象。无论哪种方式,都取决于范围,如何传递或访问它取决于您的偏好。