【发布时间】:2016-07-08 12:17:14
【问题描述】:
我是 Firebase/swift 新手,我正在尝试使用 .indexOn 对服务器上的一组节点进行排序。我已经搜索了 SO 和文档以编写规则,但我在为 swift 代码使用索引的正确格式而苦苦挣扎。
我的假设是,当我使用 .indexOn 规则时,返回的快照使用该索引对结果进行排序。通过在 swift 中使用 .queryOrderedByChild("title") 我否定了创建索引的服务器端性能优势。如果这个假设不正确,请纠正我。
这是我的 Firebase 数据库结构的简化 sn-p:
{
"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",
}
}
}
}
}
这是我的 JSON 规则:
{
"rules": {
".read": true,
".write": true,
"users": {
"$userid": {
"messages": {
".indexOn": ["title"]
}
}
}
}
}
这是我的快速代码:
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 代码以使用索引(或纠正索引如果错误)的建议都会很棒。
【问题讨论】:
-
我对你的问题并不完全清楚。添加索引后,您是否看到负面的性能影响?还是您的结果显示不正常?
-
嗨@FrankvanPuffelen - 请记住我是新手,所以我有点困惑。我的结果目前按它们存储在 Firebase 中的顺序显示,而不是按索引排序。我在文档中读到,最好使用 .indexOn 在服务器上进行排序,因此决定预先进行(我的数据集可能会变得非常大),因此我无法在此阶段评论性能。
-
好的,知道了。我会写一个答案。对于未来的问题,最好只提及出错的部分。如果数组中的元素与您期望的顺序不同,则使用索引对性能的影响是无关紧要的
-
@FrankvanPuffelen - 谢谢。我明白你的意思。但是,由于我是一个困惑的新手,我不确定我的 indexOn 是否错误,或者即使 indexOn 正确到位,我仍然应该使用 .queryOrderedByChild() 以某种方式查看预期排序顺序的结果。我想我的问题可能是:“在规则 JSON 中有一个 .indexOn 是否消除了在检索排序数据时使用 .queryOrderedByChild() 的需要?”
-
最后一个确实是一个非常明确的问题。答案是不”。下面回答。 Jad 是第一个,但你可能也需要我的。
标签: swift firebase firebase-realtime-database