Swift – Cloud Firestore中的唯一ID

前端之家收集整理的这篇文章主要介绍了Swift – Cloud Firestore中的唯一ID前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在使用Cloud Firestore生成随机唯一ID时遇到问题.以前我使用实时数据库生成随机字符串我使用childByAutoID().key.

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

正如杰伊在评论中所说,这不是重复,因为我正在尝试生成一个随机字符串,我不是想要随机文档.

解决方法

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

// 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)")
    }
}

猜你在找的Swift相关文章