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

余生长醉 提交于 2020-06-17 04:34:07

问题


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.


回答1:


Yes. Just try this:

Firestore.firestore().collection("items").document("yourId").setData(["item": "test"]) 



回答2:


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()
        }
}


来源:https://stackoverflow.com/questions/53583664/how-to-add-document-with-custom-id-to-firebase-firestore-on-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!