ios - Firebase queryOrderedByChild 不在 Swift 中按名称排序

标签 ios json swift firebase

我在安全和规则选项中有这个:

{
 "rules": {
 ".read": true,
 ".write": true,
 "groups": {
    ".indexOn": "name"
  } 
 }
}

我的 JSON 结构是这样的:

{
"groups": {
    "-KAti17inT7GbHEgbzzS": {
        "author": "ruben",
        "name": "C"
   },
   "-KAti36BQGO8sRmfufkE": {
        "author": "ruben",
        "name": "D"
   },
   "-KAti2CVAHtJllQm_m-W": {
        "author": "ruben",
        "name": "A"
   }
}

如您所见,它并没有像预期的那样按“名称”排序。 我做错了什么?

最佳答案

安全规则中的 .indexOn 指令只告诉数据库在名称属性上创建索引。它不会自动重新排序数据。

此外:JSON 对象中键的顺序是未定义的。

要按名称获取群组列表,您必须执行 Firebase 查询。来自Firebase documentation on queries :

let ref = Firebase(url:"https://dinosaur-facts.firebaseio.com/dinosaurs")
ref.queryOrderedByChild("height").observeEventType(.ChildAdded, withBlock: { snapshot in
    if let height = snapshot.value["height"] as? Double {
        println("\(snapshot.key) was \(height) meters tall")
    }
})

如果您根据您的数据结构调整此代码段,它将以正确的顺序打印组。

关于ios - Firebase queryOrderedByChild 不在 Swift 中按名称排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35506752/

相关文章:

json - 使用 jsonpath 查询从 Json 对象/数组中过滤名称

ios - RestKit复杂而简单的JSON RKObjectMapping几乎可以工作,但是

ios - 在协议(protocol)中插入 viewDidLoad()

iOS 8 : An exception was caught from the delegate of NSFetchedResultsController, 更新无效:第 0 节中的行数无效

ios - iPhone Facebook SDK 3在一个请求中获取用户详细信息和个人资料照片

ios - 有没有办法在 M1 MacBook 上直接运行 iOS 二进制文件?

ios - 如何在 Swift 5 中将电子表格数据预加载到 Core Data?

javascript - Synpress 无法使用 cy.acceptMetamaskAccess (不是函数)

ios - 如何使用Swift在UICollectionView中显示/隐藏单元格?

ios - Jenkins + Make + xcodebuild 或 Jenkins + sh + xcodebuild