【问题标题】:Why does Firebase Database queryEqualToValue not work?为什么 Firebase 数据库 queryEqualToValue 不起作用?
【发布时间】:2025-11-24 06:30:02
【问题描述】:

我正在使用以下代码构造一个FIRDatabaseQuery

let ref = FIRDatabase.database().reference()
let pointRef = ref.child("points")
let query = pointRef.queryOrderedByChild("location").queryEqualToValue(locationName)

query.observeSingleEventOfType(FIRDataEventType.ChildAdded, withBlock: { (snapshot) in
    ...
}

块被调用,但我想按每个点中的另一个字段对我的结果进行排序。由于不可能将两个queryOrderedByChilds 链接在一起,我将代码更改为以下(稍后我将链接排序查询)。

let ref = FIRDatabase.database().reference()
let pointRef = ref.child("points")
let query = pointRef.queryEqualToValue(locationName, childKey: "location")

query.observeSingleEventOfType(FIRDataEventType.ChildAdded, withBlock: { (snapshot) in
    ...
}

这里的块永远不会被调用。为什么不?我希望这些调用是相同的(除了第一个按位置排序)。

【问题讨论】:

  • let query = pointRef.child("location").queryEqualToValue(locationName)

标签: ios swift firebase-realtime-database


【解决方案1】:

看来我应该在pointRef 上调用observeSingleEventOfType,而不是query 本身。更新后的代码:

let ref = FIRDatabase.database().reference()
let pointRef = ref.child("points")
let query = pointRef.queryEqualToValue(locationName, childKey: "location")

pointRef.observeSingleEventOfType(.ChildAdded, withBlock: { (snapshot) in
    ...
}

虽然这不能解释为什么第一个代码可以工作,但第二个不能。

【讨论】:

    最近更新 更多