将 Eureka 表单数据上传到 firebase

Posted

技术标签:

【中文标题】将 Eureka 表单数据上传到 firebase【英文标题】:Upload Eureka form data to firebase 【发布时间】:2017-08-21 19:23:47 【问题描述】:

我有一个 Eureka 表单视图的用户配置文件。在这个空间中,用户可以更新他们的个人资料。如果用户点击“保存”,它会调用这个函数。

    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

如果你想在我的“viewDidLoad”中查看创建表单的代码是什么

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



我不知道是什么导致了这个错误发生!

【问题讨论】:

看起来名为 profileDisplayName 的东西是重复的。 我已经扫描了所有内容,我可以找到一个副本。 你能发布你的尤里卡表单代码吗? @BrandonMayU @ReinierMelian 我已经更新了我的问题,希望你能提供帮助! @ReinierMelian 谢谢 【参考方案1】:

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

您在代码的几个部分中都有这种行为,但我将以 name 行为例

 <<< 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

您的应用程序崩溃,因为您第二次添加了具有相同值“profileDisplayName”的Constants.PROFILE_DISPLAY_NAME

解决此问题的方法

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

NameRow 案例的固定代码

 <<< NameRow() row in

                row.title = "Name"
                row.placeholder = "Enter Name"
                row.value = PROFILE_DISPLAY_NAME
                row.tag = "\(Constants.PROFILE_DISPLAY_NAME)\(userID)"  

                

【讨论】:

以上是关于将 Eureka 表单数据上传到 firebase的主要内容,如果未能解决你的问题,请参考以下文章

如何在将图像上传到 Firebase 存储之前对其进行压缩?

使用 Flutter 将表单上传到 Firestore

将图像上传到 Firebase 存储并同时将文本输入到 Firestore 时出错

尽管没有引发错误,但图像似乎没有上传到 Firebase 存储

使用flutter将多张图片上传到firestore并获取它的下载URL并将所有URL保存到firebase

将上传的数据上传到android studio上的firebase实时数据库时应用程序崩溃