ios - Firebase 在 Swift 中检索数据

标签 ios swift firebase

我正在尝试从当前登录的用户那里检索特定数据。我的数据库中的数据如下所示:

enter image description here

例如,我只想获取全名并将其保存在变量 userName 中。以下是我用来获取数据的内容

ref.queryOrderedByChild("full_name").queryEqualToValue("userIdentifier").observeSingleEventOfType(.ChildAdded, withBlock: { snapshot in
            print(snapshot.value)
            // let userName = snapshot.value["full_name"] as! String
        })

不幸的是,这是我的控制台打印的内容。

enter image description here

如果有任何帮助,我将不胜感激 :) 谢谢!

最佳答案

它会向您显示警告消息 indexOn,因为您正在执行查询。

you should define the keys you will be indexing on via the .indexOn rule in your Security and Firebase Rules. While you are allowed to create these queries ad-hoc on the client, you will see greatly improved performance when using .indexOn

因为您知道要查找的名称,您可以直接转到该节点,而无需查询。

    let ref:FIRDatabaseReference! // your ref ie. root.child("users").child("stephenwarren001@yahoo.com")

    // only need to fetch once so use single event

    ref.observeSingleEventOfType(.Value, withBlock: { snapshot in

        if !snapshot.exists() { return }

        //print(snapshot)

        if let userName = snapshot.value["full_name"] as? String {
            print(userName)
        }
        if let email = snapshot.value["email"] as? String {
            print(email)
        }

        // can also use
        // snapshot.childSnapshotForPath("full_name").value as! String
    })

关于ios - Firebase 在 Swift 中检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37759614/

相关文章:

ios - 在 iOS 中调用 Paypal 时无法获取访问 token

ios - 使用 Swift 获取 UITableView 的当前部分

ios - 在github上推送时Xcode不添加框架

swift - 如何在 Swift 中演示僵尸对象?

java - Android有 "onInstall"方法吗?

ios - 使用两个 "modes"(UIViewController 和 UINavigationController)创建 iOS 应用程序

ios - 如何在 uilabel 中添加第二个 NSTextAttachment 图像?

ios - Objective C 的 Swift 语法

java - 根据key从firebase中获取值

ios - 使用 .Value 和 .ChildAdded 检索数据