Firebase在Swift中检索数据 [英] Firebase Retrieving Data in Swift

查看:103
本文介绍了Firebase在Swift中检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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




例如,我想只抓住full_name并将其保存在变量userName中。下面是我用来抓取我的数据

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

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



:)谢谢!

解决方案

它给了你一个警告信息 indexOn 因为你正在做一个查询。

lockquote

你应该通过.indexOn
规则来定义你要索引的键安全和Firebase规则。虽然您可以在
上创建这些客户端上的特殊查询,但在使用.indexOn时,您会看到
的性能大大提高。

如您所知,您正在查找的名称可以直接进入该节点,而无需查询。

 让ref:FIRDatabaseReference! //你的参考root.child(users)。child(stephenwarren001@yahoo.com)

//只需要一次获取所以使用单个事件

ref.observeSingleEventOfType .Value,withBlock:{snapshot in
$ b $ if if!snapshot.exists(){return}

//打印(快照)

如果让userName = snapshot.value [full_name] as?String {
print(userName)
}
如果让email = snapshot.value [email] as?String {
print(email)
}

//也可以使用
// snapshot.childSnapshotForPath(full_name)。value as!String
})


I'm trying to retrieve specific data from just the currently logged in user. My data in my database looks like this:

For example, I want to just grab the full_name and save it in a variable userName. Below is what I'm using to grab my data

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

Unfortunately, this is what my console prints.

I would appreciate any help :) Thank you!

解决方案

It gives you that warning message indexOn because you are doing a query.

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

As you know the name you are looking for you can directly go to that node, without a query.

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

这篇关于Firebase在Swift中检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆