Swift - Unique ID in Cloud Firestore

If you create the document without an explicit Id, our SDK will auto-generate a random one for you.

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

To generate random id,

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