Firebase queryEqualToValue 与 childKey [英] Firebase queryEqualToValue with childKey

查看:21
本文介绍了Firebase queryEqualToValue 与 childKey的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下方法查询 Firebase 数据库中的数据:

I am trying to query data in my Firebase database using:

queryEqual(toValue: Any?, childKey: String?)

我的数据库结构:

Schools {
    testUID1 {
        schoolNameLC: test school 1
    }
}

我的查询是:

databaseReference.child("Schools").queryEqual(toValue: "test school 1", childKey: "schoolNameLC").observe(.value) { (snap) in
        print(snap)
    }

此查询打印出 null,但我无法让它正常工作.由于我的应用程序的设置方式,我不知道键 schoolNameLCtestUID1 的父键下有一个值 testSchool1.我想要做的就是在我的数据库中搜索 Schools 并返回 schoolNameLC 值为 test school 1 的任何内容.

This query prints out null and I can't quite get it to work. Because of the way my app is set up, I don't know that the key schoolNameLC has a value testSchool1 under the parent key of testUID1. All I want to do is search through the Schools in my database and return anything with a schoolNameLC value of test school 1.

推荐答案

两个参数 queryEqualToValue:childKey:(及其兄弟 queryStartingAtValue:childKey:>queryEndingAtValue:childKey:) 是 Firebase 数据库 API 中最容易被误解的一些方法.

The two-parameter queryEqualToValue:childKey: (and its brethren queryStartingAtValue:childKey: and queryEndingAtValue:childKey:) are unfortunately some of the most misunderstood methods in the Firebase Database API.

要按子项排序然后过滤该子项的值,您必须调用queryOrderedByChild().queryEqualToValue().正如@ElCaptainV2.0 所说:

To order by a child and then filter on a value of that child, you have to call queryOrderedByChild().queryEqualToValue(). So as @ElCaptainV2.0 said:

databaseReference.child("Schools")
    .queryOrderedByChild("scho‌​olNameLC")
    .queryEqua‌​lToValue("test school 1")

如果您想从具有相同匹配test school 1<的所有节点中的特定键开始,您才应该使用childKey:参数/代码> 值.当您尝试分页/无限滚动结果时,此重载非常有用.

You only should use the childKey: parameter if you also want to start at a specific key in all nodes that have the same matching test school 1 value. This overload is really mostly useful when you're trying to paginate/endless scroll results.

这篇关于Firebase queryEqualToValue 与 childKey的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆