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 {
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]!)))
messageArray[messageArray.count - 1].messageId = messageKey
}
}
success(messages: messageArray)
}) { (error) in
}
} else {
}
, Swift, ( , ), .