Firebase and the quick use of .indexOn to sort data

I am new to Firebase / swift and I am trying to use .indexOn to sort a set of nodes on a server. I searched for SO and documents for rules, but I am afraid with the correct format for quick code to use the index.

My assumption is that when I use the .indexOn rule, the returned snapshot uses this index to order the result. Using .queryOrderedByChild ("title") in swift, I deny the server-side performance benefits of creating the index. Please correct me if this assumption is incorrect.

Here's a simplified snippet of my Firebase DB structure:

{
  "users": {
    "GkdjgdBJh1TxuAzIPezLWRouG9f2": {
      "messages": {
        "-KM0crettRl-RLQQdmMQ": {
          "body": "Anyone want a bit of body string?",
          "title": "5 Another title string",
        },
        "-KM0dhkChY6ZQ2QzuPlC": {
          "body": "This is a short body string",
          "title": "1 A lovely title string",
        },
        "-FQ0dhkChY6ZQ2Qzu3RQv": {
          "body": "Short and sweet body string",
          "title": "3 I should be in the middle",
        }
      }
    }
  }
}

Here is my JSON rule:

{
    "rules": {
        ".read": true,
        ".write": true,
        "users": {
              "$userid": {
                    "messages": {
                          ".indexOn": ["title"]
                    }
              }
        }
    }
}

Here is my quick code:

if let user = FIRAuth.auth()?.currentUser {
    // user is logged in

    // *** I think this is the line with the issue ***
    FIRDatabase.database().reference().child("users").child(user.uid).child("messages").observeEventType(.Value, withBlock: { (snapshot) in
    messageArray = []

        if let dictionaryOfMessages = snapshot.value as? [String: AnyObject] {
            for messageKey in dictionaryOfMessages.keys {
                messageArray.append(Message(json: JSON(dictionaryOfMessages[messageKey]!)))
                // set the messageId
                messageArray[messageArray.count - 1].messageId = messageKey
            }
        }
        // return the data to the VC that called the function
        success(messages: messageArray)
    }) { (error) in
        // Handle the Error
    }
} else {
    // return some generic messages about logging in etc.
}

, Swift, ( , ), .

+1
2

queryOrderedByChild, , . ;

Firebase

, , :

// My top posts by number of stars
let myTopPostsQuery = (ref.child("user-posts").child(getUid())).queryOrderedByChild("starCount")

, , . .

queryOrderedByChild . "starCount" . . .

, ;

https://firebase.google.com/docs/database/ios/retrieve-data

+2

, Jad answer , . , , .

Firebase , , , , .

. , , :

if let dictionaryOfMessages = snapshot.value as? [String: AnyObject] {

:

for child in snapshot.children {
  let key = child.key as String
  print(key)
  messageArray.append(Message(json: JSON(child.value)))  // JSON handling via SwiftyJSON
  // set the messageId
  messageArray[messageArray.count - 1].messageId = key
}
+2

All Articles