繁体   English   中英

Swift-Cloud Firestore中的唯一ID

[英]Swift - Unique ID in Cloud Firestore

我无法使用Cloud Firestore生成随机的唯一ID。 以前,我使用的是实时数据库,并使用childByAutoID().key生成随机字符串。

有没有办法为Cloud Firestore做类似的事情?

正如Jay在评论中说的那样,这不是重复的,因为我试图生成随机字符串,而不是试图获取随机文档。

如果您创建的文档没有明确的ID,我们的SDK会自动为您生成一个随机的ID。

// Add a new document with a generated id.
var ref: DocumentReference? = nil
ref = db.collection("messages").addDocument(data: [
    "sender": "<my_senders_id>",
    "recipient": "<my_recipient_id>",
    "message": "Hello from Google Cloud Platform & Firebase!",
    "status": "unread"
]) { err in
    if let err = err {
        print("Error adding document: \(err)")
    } else {
        print("Document added with ID: \(ref!.documentID)")
    }
}

要生成随机ID,

let ref = db.collection("somePath")
let docId = ref.document().documentID

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM