38

I have read the Firebase docs about priorities but I don't think I understand it yet.

I think I understand that it is related to querying and sorting data. To give my question (and the answers) some weight, in what instances might you use priorities?

From the docs I read that you can set priorities when you set a value at some reference, and then when you query that reference priority determines the ordering based on its type and value. And that makes some sense but I'm not quite understanding it.

nodebase
  • 2,510
  • 6
  • 31
  • 46

1 Answers1

71

Disclosure: I work for Firebase.

Priorities are an optional (numeric or alphanumeric) value of each node, which is used to sort the children under a specific parent or in a query if no other sort condition is specified. The priority of a node is hidden from most views of the data. In cases where a priority is specified for a node, it can be found as a .priority property in the exportVal() of a snapshot.

Since Firebase added the ability to order children on a specified property, priorities have lost most of their value. They are a left-over artifact from the time before Firebase had orderByChild queries. If you are starting on a Firebase project today, you should use orderByChild instead of relying on priorities.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 23
    Please update the docs with this info! It is very confusing why you'd need to use priorities at all in the current docs and from what you're saying they are unnecessary now we have orderByChild. – martinjbaker Apr 13 '16 at 09:17
  • 1
    The method `orderByChild` works only with strings. In cases where I have a number (eg: timestamp) stored as a key I should still use priorities. Am I right? – Isaac Bhasme Apr 30 '16 at 07:32
  • 2
    The parameter you pass into `orderByChild()` is the *name* of the property, so is naturally always a string. But you can pass any primitive JSON type into `equalTo()`, `startAt()` and `endAt()`. So they'll work perfectly for timestamps too. – Frank van Puffelen Apr 30 '16 at 14:02
  • 8
    Frank: It would be helpful for the Firebase documentation team to add this information to the docs. I am an Android developer who just started using Firebase. After carefully studying the Firebase Android docs, I was puzzled about what `orderByPriority()` offered over `orderByChild()` (apparently little or nothing) until I read this answer. – Bob Snyder Jun 04 '16 at 15:44
  • @FrankvanPuffelen Frank, is there any way to get the index/position of a particular node without loading the complete JSON. In my specific case I want the rank of a user in highscore leaderboard and the leaderboard might have millions of entries. – Rohit Goyal Jun 14 '16 at 12:46
  • 15
    Seriously document this! – Marc M. Jun 19 '16 at 08:15
  • 1
    @FrankvanPuffelen please we need documentation! another vote for it, why you ignore this? – Al-Mothafar May 16 '18 at 10:07