简体   繁体   中英

How to Add a map Value to an array in firestore Swift 5?

I am pretty new to firebase and swift. I have my FireStore DB like this . I want to add map values Reply and userID to Replies and every time I add new map value, it should append to Replies array on next Index. I know that updateDocument function will be used here, but I don't have the slightest idea of how to use that. Kindly guide me in any way possible. Thanks! Below is the sample code

@IBAction func replyPressed(_ sender: DesignableButton) {

    let db = Firestore.firestore().document("\(Path!)/All Reviews/\(docID!)")
    let replyDictionary = ["UserID": userID!, "Reply":ReplytextField.text!]

    db.updateData(/*No hint about that*/ ) { (error) in

        if error ! = nil {
            print(error?.localizedDescription)
        } else {
            print("Posted")
        }
    }
}

Well, you can use FieldValue.arrayUnion for that.

let db = Firestore.firestore().document("\(Path!)/All Reviews/\(docID!)")
let replyDictionary = ["UserID": userID!, "Reply":ReplytextField.text!]

db.updateData(["name_of_your_array" : FieldValue.arrayUnion([replyDictionary])]) { (error) in

        if let error = error {
            print(error.localizedDescription)
        } 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM