• setData
  • .editingChanged
  • HUD

setData

Overwrites the document referred to by this FIRDocumentReference. If no document exists, it is created. If a document already exists, it is overwritten.

implement func handleSave() as a selector of save button

    @objc fileprivate func handleSave() {
        guard let uid = Auth.auth().currentUser?.uid else { return }
        let docData: [String: Any] = [
            "uid": uid,
            "fullName": user?.name ?? "",
            "imageUrl1": user?.imageUrl1 ?? "",
            "age": 60,
            "profession": "Bank Robber"
        ]
        Firestore.firestore().collection("users").document(uid).setData(docData) { (err) in
            if let err = err {
                print("Failed to save user settings", err)
                return
            }
            
            print("Finished saving user info")
        }
    }

The argument type of setData() is Dictionary

여기서 만들어지는 dictionary를 바로 저장시킬 수는 없다. 그래서 예시로 임의의 값을 수동으로 넣어준다. we can’t save this dictionary right here. we should put any datas in some properties manually.

        let docData: [String: Any] = [
            "uid": uid,
            "fullName": user?.name ?? "",
            "imageUrl1": user?.imageUrl1 ?? "",
            "age": 60,
            "profession": "Bank Robber"
        ]

handleChange for: .editingChanged

UITableView의 cellForRowAt는 row의 cell을 만들어주는 오버라이드 함수이기 때문에, firestore에서 data들을 fetch한 후에 tableView를 reload를 해주었었다. 아무튼 이부분은 cell을 생성하는 부분이다. 그래서 data 업데이트와 저장이 필요한 부분의 cell에 addTarget을 하여 selector를 추가해준다. 여기서 .editingChanged를 사용하여 수정 될 때마다 호출되도록한다.

touchUpInside는 클릭했을 때 호출되는 selector의 호출 방식 중 하나이다.

because cellForRowAt is override function that generates cells for each row, we reloaded tableview after fetching datas from firestore. Anyway, this function is creating cells. So, we need to use addTarget and selector to update & save datas of cell to firebase. .editingChanged in selector is the way invoked when self target is edited.

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = SettingsCell(style: .default, reuseIdentifier: nil)
        
        switch indexPath.section {
        case 1:
            cell.textField.placeholder = "Enter Name"
            cell.textField.text = user?.name
            cell.textField.addTarget(self, action: #selector(handleNameChange), for: .editingChanged)
        case 2:
            cell.textField.placeholder = "Enter Profession"
            cell.textField.text = user?.profession
            cell.textField.addTarget(self, action: #selector(handleProfessionChange), for: .editingChanged)
        case 3:
            cell.textField.placeholder = "Enter Age"
            if let age = user?.age {
                cell.textField.text = String(age)
            }
            cell.textField.addTarget(self, action: #selector(handleAgeChange), for: .editingChanged)
        default:
            cell.textField.placeholder = "Enter Your Bio"
        }
        return cell
    }
    @objc fileprivate func handleNameChange(textField: UITextField) {
        self.user?.name = textField.text
    }
    
    @objc fileprivate func handleProfessionChange(textField: UITextField) {
        self.user?.profession = textField.text
    }
    
    @objc fileprivate func handleAgeChange(textField: UITextField) {
        self.user?.age = Int(textField.text ?? "")
    }

JGProgressHUD

        let hud = JGProgressHUD(style: .dark)
        hud.textLabel.text = "Saving settings"
        hud.show(in: view)
        Firestore.firestore().collection("users").document(uid).setData(docData) { (err) in
            hud.dismiss()
            if let err = err {
                print("Failed to save user settings", err)
                return
            }
            print("Finished saving user info")
        }

태그: ,

카테고리:

업데이트:

댓글남기기