代码之家  ›  专栏  ›  技术社区  ›  BrandonMayU

将Eureka表单数据上载到firebase

  •  2
  • BrandonMayU  · 技术社区  · 7 年前

        func saveProfileSettings(){
    
        let userID = user?.uid
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd"
    
        let PROFILE_DISPLAY_NAME_REF: NameRow? = form.rowBy(tag: Constants.PROFILE_DISPLAY_NAME)
        let PROFILE_DISPLAY_NAME = PROFILE_DISPLAY_NAME_REF?.value
    
        let PROFILE_EMAIL_REF: EmailRow? = form.rowBy(tag: Constants.PROFILE_EMAIL)
        let PROFILE_EMAIL = PROFILE_EMAIL_REF?.value
    
        let PROFILE_PHONENUMBER_REF: PhoneRow? = form.rowBy(tag: Constants.PROFILE_PHONENUMBER)
        let PROFILE_PHONENUMBER = PROFILE_PHONENUMBER_REF?.value
    
        let PROFILE_BIRTH_AGE_REF: DateRow? = form.rowBy(tag: Constants.PROFILE_BIRTH_AGE)
        let PROFILE_BIRTH_AGE_CONVERT = PROFILE_BIRTH_AGE_REF?.value
        let PROFILE_BIRTH_AGE = formatter.string(from: PROFILE_BIRTH_AGE_CONVERT!)
        let PROFILE_BIRTH_AGE_String = String(PROFILE_BIRTH_AGE)
    
        let PROFILE_GENDER_REF: SegmentedRow<String>! = form.rowBy(tag: Constants.PROFILE_GENDER)
        let PROFILE_GENDER = PROFILE_GENDER_REF.value
    
        let PROFILE_GENDER_INTEREST_REF: SegmentedRow<String>! = form.rowBy(tag: Constants.PROFILE_GENDER_INTEREST)
        let PROFILE_GENDER_INTEREST = PROFILE_GENDER_INTEREST_REF.value
    
        let uploadPath = databaseRef.child("profiles").child(userID!)
    
        let dataBlock = ["\(Constants.PROFILE_DISPLAY_NAME)": PROFILE_DISPLAY_NAME!, "\(Constants.PROFILE_EMAIL)": PROFILE_EMAIL!, "\(Constants.PROFILE_PHONENUMBER)": PROFILE_PHONENUMBER!, "\(Constants.PROFILE_BIRTH_AGE)": PROFILE_BIRTH_AGE_String!, "\(Constants.PROFILE_GENDER)": PROFILE_GENDER!, "\(Constants.PROFILE_GENDER_INTEREST)": PROFILE_GENDER_INTEREST!]
    
        print("DataBlock \(dataBlock)")
    
        uploadPath.setValue(dataBlock) { (error, datebaseRef) in
            if error != nil{
                print("Error Occured")
            }
            print("Profile Updated!")
        }
    
    
    
    }
    

    这个函数的任务是从表单中提取所有数据,将其放入数组,然后将其上载到firebase。如果用户单击“保存”并没有更新任何信息,则一切正常。但是,如果用户编辑其个人资料并单击“保存”,则会导致错误。见下文。

    assertion failed: Duplicate tag profileDisplayName: file /Users/brandonmayhew/Documents/Programming/X-Code Project's/ChristinaApp/Pods/Eureka/Source/Core/BaseRow.swift, line 172
    2017-08-21 12:19:19.258586-0700 ChristinaApp[2615:926887] assertion failed: Duplicate tag profileDisplayName: file /Users/brandonmayhew/Documents/Programming/X-Code Project's/ChristinaApp/Pods/Eureka/Source/Core/BaseRow.swift, line 172
    

    override func viewDidLoad() {
        super.viewDidLoad()
    
        self.startAnimating(Constants.animationSize, message: "Finding Your Profile", type: .ballZigZag)
    
        //findImageURL()
    
    
        //START
    
        let userID = user?.uid
        print("Logged in user: \(userID!)")
    
    
        databaseRef.child("profiles").child(userID!).observe(.value, with: { (snapshot) in
    
    
            let data = snapshot.value as? NSDictionary
    
            let PROFILE_DISPLAY_NAME = data?[Constants.PROFILE_DISPLAY_NAME] as? String
            let PROFILE_UID = data?[Constants.PROFILE_UID] as? String
            let PROFILE_IMAGE = data?[Constants.PROFILE_IMAGE] as? String
            let PROFILE_EMAIL = data?[Constants.PROFILE_EMAIL] as? String
            let PROFILE_PHONENUMBER = data?[Constants.PROFILE_PHONENUMBER] as? String
            let PROFILE_GENDER = data?[Constants.PROFILE_GENDER] as? String
            let PROFILE_GENDER_INTEREST = data?[Constants.PROFILE_GENDER_INTEREST] as? String
            let PROFILE_BIRTH_AGE = data?[Constants.PROFILE_BIRTH_AGE] as? String
            let PROFILE_LATITUDE = data?[Constants.PROFILE_LATITUDE] as? String
            let PROFILE_lONGITUDE = data?[Constants.PROFILE_lONGITUDE] as? String
    
            self.form +++ Section("Personal Information")
    
                <<< NameRow(){ row in
    
                    row.title = "Name"
                    row.placeholder = "Enter Name"
                    row.value = PROFILE_DISPLAY_NAME
                    row.tag = "\(Constants.PROFILE_DISPLAY_NAME)"
    
                    }
    
                <<< EmailRow(){ row in
    
                    row.title = "Email"
                    row.placeholder = "Enter Email"
                    row.value = PROFILE_EMAIL
                    row.tag = "\(Constants.PROFILE_EMAIL)"
    
                }
    
                <<< PhoneRow(){ row in
    
                    row.title = "Phone Number"
                    row.placeholder = "Enter Phone Number"
                    row.value = PROFILE_PHONENUMBER
                    row.tag = "\(Constants.PROFILE_PHONENUMBER)"
    
                }
    
                <<< DateRow(){ row in
                    row.title = "Your Birth Year"
    
                    //Convert 'PROFILE_BIRTH_AGE' string to NSDATE
                    let date = NSDate()
                    let dateFormatter = DateFormatter()
                    dateFormatter.dateFormat = "yyyy-MM-dd"
                    let formattedDate = dateFormatter.date(from: PROFILE_BIRTH_AGE!)
    
                    row.value = formattedDate
                    row.tag = "\(Constants.PROFILE_BIRTH_AGE)"
                }
    
    
    
                +++ Section("Dating Settings")
    
                <<< SegmentedRow <String> (){ row in
                    row.title = "I am"
                    row.options = ["Male", "Female"]
                    row.value = PROFILE_GENDER
                    row.tag = "\(Constants.PROFILE_GENDER)"
                }
    
                <<< SegmentedRow <String> (){ row in
                    row.title = "I'm interested in"
                    row.options = ["Male", "Female"]
                    row.value = PROFILE_GENDER_INTEREST
                    row.tag = "\(Constants.PROFILE_GENDER_INTEREST)"
                }
    
                +++ Section("Save Profile")
                <<< ButtonRow() {
                    $0.title = "Save"
                    }
                    .onCellSelection {  cell, row in
    
                        self.saveProfileSettings()
    
                }
    
                +++ Section("Come Back Later")
                <<< ButtonRow() {
                    $0.title = "Sign Out"
                    }
                    .onCellSelection {  cell, row in
    
                        self.signOut()
                }
    
    
    
    
            self.stopAnimating()
    
    
        })
    
        //STOP
    
    
    }
    

    1 回复  |  直到 7 年前
        1
  •  2
  •   Reinier Melian    7 年前

    您面临的问题是由添加具有相同标记字符串值的行或节引起的。在Eureka表单中,单元格甚至部分的标记应该是唯一的,因此,如果您使用相同的标记添加2行,这将导致assert异常

    你有这种行为 将行命名为示例

     <<< NameRow(){ row in
    
                    row.title = "Name"
                    row.placeholder = "Enter Name"
                    row.value = PROFILE_DISPLAY_NAME
                    row.tag = "\(Constants.PROFILE_DISPLAY_NAME)"  
    
                    }
    

    正如你所见 row.tag = "\(Constants.PROFILE_DISPLAY_NAME)" Constants.PROFILE_DISPLAY_NAME String 在项目的某些部分中定义,此字符串不会更改,并且当您将要从此方法添加第二个结果时

    databaseRef.child("profiles").child(userID!).observe(.value, with: { (snapshot) in
    

    你的应用程序崩溃,因为你正在添加 常数。PROFILE\u DISPLAY\u名称

    解决这个问题的方法

    作为这个问题的一个可能的解决方案,您可以为获取的每个用户附加一个唯一的值,比如说 userID

     <<< NameRow(){ row in
    
                    row.title = "Name"
                    row.placeholder = "Enter Name"
                    row.value = PROFILE_DISPLAY_NAME
                    row.tag = "\(Constants.PROFILE_DISPLAY_NAME)\(userID)"  
    
                    }