【问题标题】:Apply filter and sorting same time firebase [duplicate]应用过滤器并同时排序firebase [重复]
【发布时间】:2017-01-19 11:01:19
【问题描述】:

我的 firebase json 树是这样的

{
"-KZVOY6qU29fJlRSwRNo" : {
    "fedStateDetails" : "Fasted",
    "targetkcal" : 596,
    "timestamp" : 1.482307285673975E9,
    "userid" : "816",
    "workout_id" : "123"
  },
  "-KZVSmeW94XetROs0nsN" : {
    "fedStateDetails" : "Fasted",
    "targetkcal" : 1874,
    "timestamp" : 1.482308270356936E9,
    "userid" : "47",
    "workout_id" : "129"
  },
  "-KZWGdx-j1q7J37x0wck" : {
    "fedStateDetails" : "Fasted",
    "targetkcal" : 0,
    "timestamp" : 1.482321866382767E9,
    "userid" : "47",
    "workout_id" : "40"
  }
}

所以我只需要获取“userid”为 47 且“timestamp”在给定范围之间的用户,我的代码如下所示。

let startTime = Date().timeIntervalSince1970 - 10800.0
        let endTime = Date().timeIntervalSince1970
        self.dbRef.child(FireBaseTable.workOutInfoTable)
            .queryOrdered(byChild: "timestamp")
            .queryStarting(atValue: startTime, childKey: "timestamp")
            .queryEnding(atValue: endTime, childKey: "timestamp")
            .queryEqual(toValue: "47")
            .observe(.value) { (snapshot1 :FIRDataSnapshot?) in
            if let value : Dictionary<String,AnyObject> = snapshot1?.value as? Dictionary<String,AnyObject>
            {
                print("*********")
                print(value)
            }
        }

但我不能同时应用两个 order by。请帮忙。 提前致谢。

【问题讨论】:

  • 不,不可能在 firebase 中同时添加 2 个订单...在获取数据后,您必须通过其他方式对其进行排序
  • @EICaptainv2.0 是的,我知道,但如何获得预期的结果?有什么建议吗?会很有帮助的。

标签: ios swift xcode firebase firebase-realtime-database


【解决方案1】:

如果大多数时候您需要访问这些项目,这将是每个用户,您可以更改您的数据模型:

{
"user816": {
  "-KZVOY6qU29fJlRSwRNo" : {
    "fedStateDetails" : "Fasted",
    "targetkcal" : 596,
    "timestamp" : 1.482307285673975E9,
    "workout_id" : "123"
  }
},
"user47": {
  "-KZVSmeW94XetROs0nsN" : {
    "fedStateDetails" : "Fasted",
    "targetkcal" : 1874,
    "timestamp" : 1.482308270356936E9,
    "workout_id" : "129"
  },
  "-KZWGdx-j1q7J37x0wck" : {
    "fedStateDetails" : "Fasted",
    "targetkcal" : 0,
    "timestamp" : 1.482321866382767E9,
    "workout_id" : "40"
  }
}
}

现在您可以使用child() 访问特定用户的数据,然后使用queryOrder(byChild:"timestamp") 按时间戳对其进行排序。

我推荐:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-06
    • 2018-01-16
    • 2018-11-21
    • 1970-01-01
    相关资源
    最近更新 更多