Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Document with Custom ID to Firebase (Firestore) on Swift

Is there any way to add a document to firestore collection with custom id on Swift Language, not the id generated by firestore engine.

Thanks in advance for your responses.

like image 678
racso Avatar asked Dec 02 '18 19:12

racso


2 Answers

Yes. Just try this:

Firestore.firestore().collection("items").document("yourId").setData(["item": "test"]) 
like image 158
Vasil Garov Avatar answered Sep 19 '22 13:09

Vasil Garov


2020

Usually you'd want the error handling:

Firestore.firestore()
    .collection("companyAddress")
    .document(companyID)
    .setData([
        "address1": a1,
        "address2": a2,
        "city": c
    ]) { [weak self] err in
        guard let self = self else { return }
        if let err = err {
            print("err ... \(err)")
            self.yourErrorAlert()
        }
        else {
            print("saved ok")
            self.proceedWithUX()
        }
}
like image 20
Fattie Avatar answered Sep 20 '22 13:09

Fattie