简体   繁体   English

不要将所有文本字段更新为 firebase

[英]Don't update all textfields to firebase

I have three textFields and when the user enters information into them, the information is sent to Firebase .我有三个textFields ,当用户向其中输入信息时,信息会发送到Firebase

     func buttonMethod() { 
if let uid = FIRAuth.auth()?.currentUser?.uid{
            if let City = City.text{
                if let Major = Major.text{
                    if let College = College.text{

                        let saveObject: Dictionary<String, Any> = [
                            "uid": uid,
                            "City" : City,
                            "Major" : Major,
                            "College" : College
                        ]
                   }
               }
          }
      }



FIRDatabase.database().reference().child("info").child(self.loggedInUser!.uid).setValue(saveObject)

Using those same textFields , the user can edit their information.使用相同的textFields ,用户可以编辑他们的信息。 And the information will be displayed in labels in another view controller.并且信息将显示在另一个视图控制器的标签中。

  func loadInfo(){
        if (FIRAuth.auth()?.currentUser) != nil{

            FIRDatabase.database().reference().child("info").child(self.loggedInUser!.uid).observeSingleEvent(of: .value, with: { (snapshot:FIRDataSnapshot) in


                if let postsDictionary = snapshot .value as? [String: AnyObject] {
                    for post in postsDictionary {
                        //self.posts.add(post.value)

                        let city = postsDictionary["City"] as! String
                        let major = postsDictionary["Major"] as! String
                        let college = postsDictionary["College"] as! String
                        self.City.text = city
                        self.Major.text = major
                        self.College.text = college
                    }

                }})}
    }

My problem is that when the user edits the information, if a textField was not edited it replaces the value of the non-edited textField in Firebase with an empty string.我的问题是,当用户编辑信息时,如果未编辑textField ,它会将Firebase未编辑的textField的值替换为空字符串。 I want it to be that if the user does not edit a textField then the value in Firebase should stay the same.I started out with this:我希望如果用户不编辑textField那么Firebase的值应该保持不变。我从这个开始:

 if City.tex t== "" {

 }
 if Major.text=="" {

 }
 if College.text==""{

 }

But I'm not sure where to go from there.但我不确定从那里去哪里。

If the textfield text is empty then don't send that field to firebase.如果文本字段文本为空,则不要将该字段发送到 firebase。 Say if user haven't edited city field then don't add city textField in the method to send value to firebase.假设用户尚未编辑城市字段,则不要在方法中添加城市 textField 以将值发送到 firebase。

Please try update your buttonMethod() like this,请尝试像这样更新您的buttonMethod()

func buttonMethod() {
    if let uid = FIRAuth.auth()?.currentUser?.uid{

        var tempObject = [String:Any]()
        tempObject["uid"] = uid
        if !City.text.isEmpty {
            tempObject["City"] = City.text
        }

        if !Major.text.isEmpty{
            tempObject["Major"] = Major.text
        }

        if !College.text.isEmpty{
            tempObject["College"] = College.text
        }

        let saveObject:Dictionary<String,Any> = tempObject

}

Instead of saving the object to firebase update the data at firebase using updateChildValues() function使用updateChildValues()函数更新updateChildValues()的数据,而不是将对象保存到updateChildValues()

I think there is another solution: you could show the original values/information on your text fields instead of leaving them blank.我认为还有另一种解决方案:您可以在文本字段中显示原始值/信息,而不是将它们留空。 So try to remove the textfields in your code with the following structure:因此,尝试使用以下结构删除代码中的文本字段:

import Foundation
import SwiftUI

struct TextFieldWithDefaultValue: View {

    var model:userModel    // Replace it with your own view model
    var textFieldName: String
    @State var editedValue: String

    init(model: userModel, text: String) {
        self.model = model
        self.textFieldName = text
        switch self.textFieldName {
        case "Name": self._editedValue = State(wrappedValue: model.getName())
        case "Bio": self._editedValue = State(wrappedValue: model.getBio())
        case "Interest": self._editedValue = State(wrappedValue: model.getInterest())
        default:
            self._editedValue = State(wrappedValue: "No records")
        }
    }
    
    var body: some View {
        TextField(textFieldName, text: $editedValue)
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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