Snippets Collections
   
 StructureName.getDocument("Users", "") { (document, gotDocument) in
    
            if gotDocument {
                print("Success")
            } else {
                print("Error while getting Document")
            }
        }

    public static func getDocument(_ collectionName: String, _ documentId : String, completion: @escaping(_ documentSnapShot : DocumentSnapshot,_ success : Bool) -> Void) {
        
            SVProgressHUD.show()
            let User = Auth.auth().currentUser
            guard let user = User else {
                SVProgressHUD.dismiss()
                return
            }
     
        var docID = ""
        if documentId == "" {
            docID = user.uid
        } else {
            docID = documentId
        }

            let db = Firestore.firestore()
        db.collection(collectionName).document(docID).addSnapshotListener { (document, error) in
                if error != nil {
                    print("Error :", error as Any)
                    SVProgressHUD.dismiss()
                    return
                }
                
                guard let document = document else {
                    print("Error While Getting Document")
                    SVProgressHUD.dismiss()
                    return
                }
                completion(document, true)
            }
    }
   
 FireStore.getDocument("Users", "") { (document, gotDocument) in
    
            if gotDocument {
                print("Success")
            } else {
                print("Error while getting Document")
            }
        }

    public static func getDocument(_ collectionName: String, _ documentId : String, completion: @escaping(_ documentSnapShot : DocumentSnapshot,_ success : Bool) -> Void) {
        
            SVProgressHUD.show()
            let User = Auth.auth().currentUser
            guard let user = User else {
                SVProgressHUD.dismiss()
                return
            }
     
        var docID = ""
        if documentId == "" {
            docID = user.uid
        } else {
            docID = documentId
        }

            let db = Firestore.firestore()
            db.collection(collectionName).document(docID).getDocument { (document, error) in
                
                if error != nil {
                    print("Error :", error as Any)
                    SVProgressHUD.dismiss()
                    return
                }
                
                
                guard let document = document else {
                    print("Error While Getting Document")
                    SVProgressHUD.dismiss()
                    return
                }
                completion(document, true)
            }
    }
// 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()
        }
    }
// calling      
UpdateProfileData(dataToUpdate) { (isUpdated) in
            if isUpdated {
                self.aboutTableView.reloadData()
                print("updatedd")
            }
        }

func UpdateProfileData (_ dateToUpdate: [String: Any], completionHandler:@escaping (_ success:Bool) -> Void) {
        
        SVProgressHUD.show()
        let User = Auth.auth().currentUser
        guard let user = User else {
            print("Error45")
            SVProgressHUD.dismiss()
            return
        }
        let db = Firestore.firestore()
        db.collection("Users").document(user.uid).setData(dateToUpdate, merge: true) { (error) in
            if error != nil {
                print("Error99")
                SVProgressHUD.dismiss()
                return
            }
            print("updated")
            completionHandler(true)
            SVProgressHUD.dismiss()
        }
    }
// calling 
    getProfileMode { (mode) in
            print("Mode : ", mode)
        }

func getProfileMode(completion: @escaping (String) -> Void) {
           // write firestore code here
            completion("true")
        }
star

Wed Jun 15 2022 10:51:10 GMT+0000 (Coordinated Universal Time)

#ios #swift #closure #document #get
star

Wed Jun 15 2022 10:50:25 GMT+0000 (Coordinated Universal Time)

#ios #swift #closure #document #get
star

Wed Jun 15 2022 07:24:22 GMT+0000 (Coordinated Universal Time)

#ios #swift #closure #update #document
star

Wed Jun 15 2022 07:10:53 GMT+0000 (Coordinated Universal Time)

#ios #swift #closure
star

Thu Jun 09 2022 10:49:22 GMT+0000 (Coordinated Universal Time)

#ios #swift #closure #return

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension