Update any Document (best closure)
Wed Jun 15 2022 07:24:22 GMT+0000 (Coordinated Universal Time)
Saved by
@hasnat
#ios
#swift
#closure
#update
#document
// calling
StrutName.updateDocument("Users", "", dataToUpdate) { (isUpdated) in
if isUpdated {
print("Success")
} else {
print("Found Error While Updating")
}
}
public static func updateDocument (_ collectionName: String, _ documentId : String, _ dataToUpdate: [String: Any], completionHandler:@escaping (_ success:Bool) -> Void) {
var documentID = ""
SVProgressHUD.show()
let User = Auth.auth().currentUser
guard let user = User else {
SVProgressHUD.dismiss()
completionHandler(false)
return
}
if documentId == "" {
documentID = user.uid
} else {
documentID = documentId
}
let db = Firestore.firestore()
db.collection(collectionName).document(documentID).setData(dataToUpdate, merge: true) { (error) in
if error != nil {
completionHandler(false)
SVProgressHUD.dismiss()
return
}
print("updated321")
completionHandler(true)
SVProgressHUD.dismiss()
}
}
content_copyCOPY
You can update any document by calling updateDocument Function. while calling you have to pass collectionName, documentId (if document id is not user id) and data that you want to update.
Comments