【问题标题】:Firebase retrieve data below auto ID In SwiftFirebase 在 Swift 中检索低于自动 ID 的数据
【发布时间】:2017-06-22 02:46:46
【问题描述】:

我无法从 Firebase 检索数据。

我想在 auto ID 下读取 JSON 中的所有 contactName 数据,然后附加到 UIPickerView。

这是我的 JSON 树(使用 childByAutoId())

这是我的 Swift 代码

dbRef = Database.database().reference()

dbRef.child("user").child("contacts").queryOrdered(byChild: "contactName").observeSingleEvent(of: .value, with: {(snapshot) in

    for snap in snapshot.children {

        let userSnap = snap as! DataSnapshot

        let contactName = userSnap.value as? String

            self.pickOption.append("\(contactName)")
        }
    })

但结果显示我所有的 nil 数据...看起来像这样。

我该如何解决它..?

【问题讨论】:

  • 尝试记录 useSnap.value。你会知道问题出在哪里

标签: ios swift firebase firebase-realtime-database


【解决方案1】:

我自己解决了!

但首先,我决定不使用 UIPickerView。

我想做的是在自动 ID 下方添加数据。

我不确定这是解决这个问题的好算法,但无论如何,我做到了:)

dbRef.child("user/contacts/").observe(.value, with: {(snapshot) in

    if let result = snapshot.children.allObjects as? [DataSnapshot] {

            for child in result {

                let orderID = child.key as String //get autoID

                self.dbRef.child("user/contacts/\(orderID)/contactName").observe(.value, with: { (snapshot) in

                    if let nameDB = snapshot.value as? String {

                        if self.debtorName == nameDB {

                            self.dbRef.child("user/contacts/\(orderID)").updateChildValues(data)
                        }
                    }
                })
            }
        }
    })

【讨论】:

    猜你喜欢
    • 2016-09-01
    • 2021-11-28
    • 1970-01-01
    • 1970-01-01
    • 2017-05-29
    • 1970-01-01
    • 1970-01-01
    • 2018-03-20
    • 1970-01-01
    相关资源
    最近更新 更多