3

I am trying to find a way I could have a query for the users in my application. For example, if my user want to search for a user to add him as a friend. I think I can save all the users in the Firebase database, and then perform a query in the database, but it look very inefficient(and there must be a easier built in method to do it).

Idan Aviv
  • 1,253
  • 2
  • 13
  • 23
  • Why inefficient? If you have a list of users and you want to perform a search a query has to be made by someone. Magicians don't exist :D – lubilis Jun 13 '16 at 16:00
  • 1
    u could always guess, and if u are correct every time that'll be O(1) – rigdonmr Jun 13 '16 at 16:09
  • My question is if there is a user query, like the one in parse where you can create a query to find a user (using id, username, or email). Does this exist in Firebase, or do I need to put all the users on the real time database, and then query in this database. – Idan Aviv Jun 13 '16 at 16:17
  • This is a basic query in firebase. Keep in mind there are no 'users' in Firebase as such. If you want to keep users names, emails, etc you would keep those in a /users node, and then query on that node for the specific user you are looking for. – Jay Jun 13 '16 at 17:57
  • There is no built-in API to query the users in Firebase Authentication. That's why many developers also store information about their users in their Firebase Database. See http://stackoverflow.com/questions/37449635/how-do-i-query-other-firebase-users, http://stackoverflow.com/questions/33737009/how-do-you-search-access-users-data/33738253#33738253 and http://stackoverflow.com/questions/14673708/how-do-i-return-a-list-of-users-if-i-use-the-firebase-simple-username-password/14676121#14676121 – Frank van Puffelen Jun 13 '16 at 18:12
  • Thank You for the quick respond, I am now storing my information on the database as well. I have now a problem in checking if a username is a unique --- http://stackoverflow.com/questions/37824590/check-if-user-exist-with-firebase-3-0-swift – Idan Aviv Jun 15 '16 at 01:50

1 Answers1

0

".indexOn" is the solution for you!!

If you structure you date like below you would be able to query user!! Here you are able to query user on full_name and reversed_full_name. You can provide whichever child you want to query on. For instance, you can add email field under people and then add "email" in ".indexOn"!!!

"people": {
  ".indexOn": ["_search_index/full_name", "_search_index/reversed_full_name"],
  ".read": true,
  "$uid": {
    ".write": "auth.uid === $uid",
    "full_name": {
      ".validate": "newData.isString()"
    },
    "profile_picture": {
      ".validate": "newData.isString()"
    },
    "posts": {
      "$postId": {
        ".validate": "newData.val() === true && newData.parent().parent().parent().parent().child('posts').child($postId).exists()"
      }
    },
    "_search_index": {
      "full_name": {
        ".validate": "newData.isString()"
      },
      "reversed_full_name": {
        ".validate": "newData.isString()"
      }
    },
    "following": {
      "$followedUid": {
        ".validate": "newData.parent().parent().parent().parent().child('followers').child($followedUid).child($uid).val() === true" // Makes sure /followers is in sync
      }
    }
  }

Hope this helps. Here's the link to whole security rules, which is provided from Firebase https://github.com/firebase/friendlypix/blob/master/web/database-rules.json

user2884707bond
  • 559
  • 4
  • 24