Swift - Cloud Firestore 中的唯一 ID
Swift - Unique ID in Cloud Firestore
我在使用 Cloud Firestore 生成随机唯一 ID 时遇到问题。以前我使用实时数据库并生成随机字符串我使用 childByAutoID().key
.
有没有办法为 Cloud Firestore 做类似的事情?
正如 Jay 在评论中所说,这不是重复项,因为我正在尝试生成随机字符串,而不是尝试获取随机文档。
如果您创建的文档没有明确的 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)")
}
}
生成随机id,
let ref = db.collection("somePath")
let docId = ref.document().documentID
我在使用 Cloud Firestore 生成随机唯一 ID 时遇到问题。以前我使用实时数据库并生成随机字符串我使用 childByAutoID().key
.
有没有办法为 Cloud Firestore 做类似的事情?
正如 Jay 在评论中所说,这不是重复项,因为我正在尝试生成随机字符串,而不是尝试获取随机文档。
如果您创建的文档没有明确的 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)")
}
}
生成随机id,
let ref = db.collection("somePath")
let docId = ref.document().documentID