使用Firebase(Swift)按名称或电子邮件地址查询用户 [英] Query users by name or email address using Firebase (Swift)

查看:163
本文介绍了使用Firebase(Swift)按名称或电子邮件地址查询用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于 Firebase Swift ,我很新,在查询时遇到了一些麻烦。
所以基本上有两件事情我想要做:


  1. 查询我的用户,查找那些在其名称(或电子邮件地址)中包含某个字符串,并将它们添加到数组中。

  2. 获取所有用户并将其添加到数组中。

这个问题的相关部分是这样的:

正如您所看到的,我正在使用Firebase的简单登录(稍后我想也添加了Facebook登录),并通过他们的用户名存储我的用户。

我的规则文件的一部分看起来像这样:

 registered_users:{
.read:true,
.write:true,
.indexOn :[name]
}

所以每个人都应该有读写权限我的数据的一部分。



我还阅读了Firebase的正在检索数据部分iOS指南在他们的网站上,并根据该指南,我的代码获取所有的用户名称和电子邮件地址应该工作,至少我是这样认为的。但事实并非如此。这是我的代码:

  func getUsersFromFirebase(){
let registeredUserRef = firebaseRef.childByAppendingPath(registered_users)
registeredUserRef.queryOrderedByChild(name)。observeSingleEventOfType(.Value,withBlock:{snapshot in
if let email = snapshot.value [email] as?String {
println(如果让name = snapshot.value [name] as?String {
println(\ (snapshot.key)具有名称:\(name))
}
})
}

我注意到,在firebase指南中,他们总是使用ChildAdded而不是Value类型,但对我来说,Value更有意义。 Value的输出是什么都没有,ChildAdded的输出只有一个用户,即现在登录的用户。



所以我的问题是:


  1. 我可以用我目前的数据结构来做这个查询,还是必须通过他们的uid去除用户的故事?
  2. 如果是,我将如何更改我的代码,使其工作?

  3. 如果不是,最好的方式来存储我的用户,并通过查询他们名称可能?

  4. 如何查询例如鼓起,只得到用户simplelogin:1(Max Mustermann)?

我希望我的描述足够详细。 Thx提前为您提供帮助。

补充

奇怪的是,检索数据指南说,按高度查询和排序以下数据是可能的。

数据:


查询代码:

和我打算做的不一样吗?

解决方案

我遇到过类似的情况,从子节点拉出数据。



https://groups.google.com/d/msg/firebase-talk/Wgaf-OIc79o/avhmN97UgP4J



首先我可以推荐的是不要将Firebase查询视为SQL查询,因为它们不是。他们就像一个轻量级的查询。其次,如果你想查询,你需要压扁你的数据,因为查询只能深入一层(不能真正的在子笔记中查询数据)



最后 - 如果您不想拼凑您的数据,可以使用一个概念选项来回答您的问题;


  1. 如果可能,
    注册用户节点上的ObserveSingleEventOfType:FEventTypeValue。这将读取所有注册用户的快照。
  2. 迭代快照并将每个用户读入数组(作为字典对象)
  3. 然后使用NSPredicate提取所需的用户数组。

我已经运行了大量测试和性能,除非你有数以千计的用户。

希望有所帮助!


I'm quite new to Firebase and Swift and I'm having some trouble when it comes to querying. So there are basically two things I'd like to do:

  1. Query my users and find only those that contain a certain String in their name (or email address) and add them to an array.
  2. Get all of my users and add them to an array.

The relevant part of my data for this question looks like this:

As you can see, I'm using the simplelogin of Firebase (later I'd like too add Facebook login) and I'm storing my users by their uid.

A part of my rules file looks like this:

"registered_users": {
    ".read": true,
    ".write": true,
    ".indexOn": ["name"]
 }

So everybody should have read and write access to this part of my data.

I also read the "Retrieving Data" part of the Firebase iOS Guide on their website and according to that guide, my code on getting all the users names and email addresses should work, at least I think so. But it doesn't. Here is my code:

func getUsersFromFirebase() {
        let registeredUserRef = firebaseRef.childByAppendingPath("registered_users")
        registeredUserRef.queryOrderedByChild("name").observeSingleEventOfType(.Value, withBlock: { snapshot in
            if let email = snapshot.value["email"] as? String {
                println("\(snapshot.key) has Email: \(email)")
            }
            if let name = snapshot.value["name"] as? String {
                println("\(snapshot.key) has Name: \(name)")
            }
        })
    }

I noticed, that in the firebase guide, they always used the type ChildAdded and not Value, but for me Value makes more sense. The output with Value is nothing and the output with ChildAdded is only one user, namely the one that is logged in right now.

So my questions are:

  1. Can I do this query with my current data structure or do I have to get rid of storying the users by their uid?
  2. If yes, how would I have to change my code, to make it work?
  3. If no, what would be the best way to store my users and make querying them by name possible?
  4. How can I query for e.g. "muster" and get only the user simplelogin:1 (Max Mustermann)?

I hope my description is detailed enough. Thx in advance for your help.

Supplement:

The weird thing is, that the "Retrieving Data" guide says, that querying and sorting the following data by height is possible.

Data:

Querying code:

And isn't that exactly the same that I intent to do?

解决方案

I have run into similar situations where I wanted to pull out data from child nodes.

https://groups.google.com/d/msg/firebase-talk/Wgaf-OIc79o/avhmN97UgP4J

The first thing I can recommend is to not think of Firebase query's as SQL queries as they are not. They are like a light duty query.

Secondly, you need to flatten your data if you want to query, as a query only goes one level deep (can't really query data in child notes)

Lastly - if you don't want to flatten your data, one conceptual option to answer your question;

  1. If possible, ObserveSingleEventOfType:FEventTypeValue on the registered users node. This will read all of the registered users into a snapshot.
  2. Iterate over the snapshot and read each user into an array (as dictionary objects)
  3. Then use NSPredicate to extract an array of users that you want.

I've run numerous tests and performance wise, it's negligible unless you have thousands of users.

Hope that helps!

这篇关于使用Firebase(Swift)按名称或电子邮件地址查询用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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