同时应用过滤器和排序firebase

时间:2017-01-19 11:01:20

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

我的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和"时间戳"在给定范围之间,我的代码如下所示。

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)
            }
        }

但我不能一起申请两个订单。请帮忙。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

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

{
"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")按时间戳排序。

我建议: