使用Firebase (Swift)按用户名或电子邮件地址查询用户

时间:2022-02-17 21:24:54

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:

我对Firebase和Swift都很陌生,我在查询时遇到了一些麻烦。我想做两件事:

  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. 查询我的用户,只找到那些在其名称(或电子邮件地址)中包含特定字符串的用户,并将其添加到数组中。
  3. Get all of my users and add them to an array.
  4. 获取所有用户并将其添加到数组中。

The relevant part of my data for this question looks like this: 使用Firebase (Swift)按用户名或电子邮件地址查询用户

关于这个问题,我的相关数据如下:

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.

正如您所看到的,我使用的是Firebase的simplelogin(稍后我还想添加Facebook登录),我通过用户的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:

我还在他们的网站上阅读了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("\(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.

我注意到,在firebase指南中,他们总是使用ChildAdded类型,而不是Value,但是对我来说Value更有意义。带有值的输出是空的,带有childadd的输出只有一个用户,也就是现在登录的那个用户。

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. 我是否可以使用当前的数据结构来执行此查询,还是必须通过用户的uid来避免存储用户?
  3. If yes, how would I have to change my code, to make it work?
  4. 如果是,我要如何更改代码,才能使它工作?
  5. If no, what would be the best way to store my users and make querying them by name possible?
  6. 如果没有,那么存储我的用户并使按用户名查询成为可能的最佳方式是什么?
  7. How can I query for e.g. "muster" and get only the user simplelogin:1 (Max Mustermann)?
  8. 我如何查询……“集合”,只得到用户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:

数据:

使用Firebase (Swift)按用户名或电子邮件地址查询用户

Querying code: 使用Firebase (Swift)按用户名或电子邮件地址查询用户

查询代码:

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

这和我想做的不完全一样吗?

2 个解决方案

#1


2  

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

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.

我建议的第一件事是不要把Firebase查询看作是SQL查询。它们就像一个简单的查询。

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. 如果可能的话,在注册用户节点上观察到FEventTypeValue。这将把所有注册用户读入快照。
  3. Iterate over the snapshot and read each user into an array (as dictionary objects)
  4. 在快照上进行迭代,并将每个用户读入一个数组(作为dictionary对象)
  5. Then use NSPredicate to extract an array of users that you want.
  6. 然后使用NSPredicate提取所需的用户数组。

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

我已经运行了大量的测试和性能,除非您有成千上万的用户,否则它是可以忽略的。

Hope that helps!

希望会有帮助!

#2


-1  

To answer your questions

回答你的问题

1) Yes, you can query with your current structure. A query can go 1 child deep, but not within a child's children.

是的,你可以用你现在的结构进行查询。查询可以深入到一个子元素中,但不能深入到子元素中。

2) If yes, how would I have to change my code, to make it work?

2)如果是的话,我该如何修改我的代码,让它工作呢?

Here's a quickie that queries by a users last name:

这里有一个快速查询的用户的姓氏:

Firebase *usersNodeRef = your users node 'registered_users'
FQuery *allUsersRef = [usersNodeRef queryOrderedByChild:@"lastName"];
FQuery *specificUserRef = [allUsers queryEqualToValue:@"aLastName"];

[specificUser observeEventType:FEventTypeChildAdded withBlock:^(FDataSnapshot *snapshot) {

  NSDictionary *dict = snapshot.value;
  NSString *key = snapshot.key;

  NSLog(@"key = %@ for child %@", key, dict);

}];

How can I query for e.g. "muster" and get only the user simplelogin:1 (Max Mustermann)?

我如何查询……“集合”,只得到用户simplelogin:1 (Max Mustermann)?

In your uses node structure, the users are store in nodes with a key.. the key is the simplelogin:1 etc. snapshot.key will reveal that. So it's key/value pair deal...

在您的使用节点结构中,用户使用一个键存储在节点中。关键是simplelogin:1等快照。键会显示。所以这是关键/价值对交易……

value = snapshot.value key = snapshot.key

值=快照。值键= snapshot.key

#1


2  

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

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.

我建议的第一件事是不要把Firebase查询看作是SQL查询。它们就像一个简单的查询。

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. 如果可能的话,在注册用户节点上观察到FEventTypeValue。这将把所有注册用户读入快照。
  3. Iterate over the snapshot and read each user into an array (as dictionary objects)
  4. 在快照上进行迭代,并将每个用户读入一个数组(作为dictionary对象)
  5. Then use NSPredicate to extract an array of users that you want.
  6. 然后使用NSPredicate提取所需的用户数组。

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

我已经运行了大量的测试和性能,除非您有成千上万的用户,否则它是可以忽略的。

Hope that helps!

希望会有帮助!

#2


-1  

To answer your questions

回答你的问题

1) Yes, you can query with your current structure. A query can go 1 child deep, but not within a child's children.

是的,你可以用你现在的结构进行查询。查询可以深入到一个子元素中,但不能深入到子元素中。

2) If yes, how would I have to change my code, to make it work?

2)如果是的话,我该如何修改我的代码,让它工作呢?

Here's a quickie that queries by a users last name:

这里有一个快速查询的用户的姓氏:

Firebase *usersNodeRef = your users node 'registered_users'
FQuery *allUsersRef = [usersNodeRef queryOrderedByChild:@"lastName"];
FQuery *specificUserRef = [allUsers queryEqualToValue:@"aLastName"];

[specificUser observeEventType:FEventTypeChildAdded withBlock:^(FDataSnapshot *snapshot) {

  NSDictionary *dict = snapshot.value;
  NSString *key = snapshot.key;

  NSLog(@"key = %@ for child %@", key, dict);

}];

How can I query for e.g. "muster" and get only the user simplelogin:1 (Max Mustermann)?

我如何查询……“集合”,只得到用户simplelogin:1 (Max Mustermann)?

In your uses node structure, the users are store in nodes with a key.. the key is the simplelogin:1 etc. snapshot.key will reveal that. So it's key/value pair deal...

在您的使用节点结构中,用户使用一个键存储在节点中。关键是simplelogin:1等快照。键会显示。所以这是关键/价值对交易……

value = snapshot.value key = snapshot.key

值=快照。值键= snapshot.key