Request users by name or email address using Firebase (Swift)

I am new to Firebase and Swift , and I am having some problems when it comes to queries. So, there are two main things that I would like to do:

  • Request my users and find only those that contain a specific string in their name (or email address), and add them to the array.
  • Get all my users and add them to the array.

The relevant part of my data for this question looks like this: enter image description here

As you can see, I use simplelogin Firebase (later I would also add Facebook to login), and I save my users by their uid.

Part of my rules file is as follows:

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

Therefore, everyone should have access to this part of my data for reading and writing.

I also read the “Data Extraction” part of the iOS Firebase Guide on my website, and in accordance with this guide, my code to get all usernames and email addresses should work, at least I think so. But this is not so. 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 manual they always used the ChildAdded type, not the value, but for me the value makes more sense. The result with the value is nothing, and the output from ChildAdded is only one user, namely the one that is registered right now.

So my questions are:

  • Can I execute this query with my current data structure or do I need to get rid of user stories by their uid?
  • If so, how do I change the code to make it work?
  • If not, what would be the best way to store my users and make a query by name possible?
  • How can I request, for example, to "collect" and get only the user simplelogin: 1 (Max Mustermann)?

I hope my description is quite detailed. Thanks in advance for your help.

Addition

It is strange that the manual "Data extraction" says that it is possible to query and sort the following data by height.

Data:

enter image description here

Request Code: enter image description here

And isn't that what I intend to do?

+6
source share
2 answers

I faced similar situations when I wanted to pull data from child nodes.

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

The first thing I can recommend is not to think of the Firebase query as SQL queries, since that is not the case. They look like an easy request.

Secondly, you need to smooth your data if you want to query, since the query goes only one level (cannot really request data in child notes)

Finally - if you do not want to smooth your data, one conceptual answer to your question;

  • If possible, ObserveSingleEventOfType: FEventTypeValue on registered node users. This will remember all registered users in the snapshot.
  • Iterate through a snapshot and read each user into an array (like dictionary objects)
  • Then use NSPredicate to retrieve the array of users you want.

I checked numerous tests and performance, this is not significant if you do not have thousands of users.

Hope this helps!

+2
source

To answer your questions

1) Yes, you can request your current structure. The request can go 1 child, but not inside child children.

2) If so, how do I change the code to make it work?

Here's a quickie requesting a username:

 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 request, for example, to "collect" and get only the user simplelogin: 1 (Max Mustermann)?

In your node structure, users are stored in nodes with a key. The key is a simple login: 1, etc. snapshot.key will show this. So a key / value pair ...

value = snapshot.value key = snapshot.key

-1
source

Source: https://habr.com/ru/post/983872/


All Articles