Firebase:获取对第一项/元素的引用 [英] Firebase: Get reference to first item/element

查看:60
本文介绍了Firebase:获取对第一项/元素的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取Firebase数组"中第一项的引用

I'm trying to get a reference to the fist item in a firebase "Array"

在我的情况下为"ElmmycgjS1Nvq.../albums/-IrdMMds"

但是我不确定如何构造查询:

But I'm not sure how to contsturct the query:

dbRef.child("ElmmycgjS.../albums").???.child("name"); // = "Brothers in Arms"

其中???应该类似于getFirst(). 假设我事先不知道第一张专辑(-IrdMMds)的Id.我只需要拿到第一个.我已经尝试过limitToFirst(1).getRef(),但这没有帮助.

Where ??? should be something like getFirst(). Suppose I don't know the Id of the first Album beforehand (-IrdMMds). I just need to get the first one. I've tried limitToFirst(1).getRef() but this didn't help.

推荐答案

您需要一个监听第一个孩子的查询:

You'll want a query that listens for the first child:

Query first = dbRef.child("ElmmycgjS.../albums").limitToFirst(1);

然后将侦听器附加到该查询.

And then attach a listener to that query.

当您对Firebase数据库执行查询时,可能会有多个结果.因此,查询结果将是这些结果的列表.即使只有一个结果,它也将是一个结果的列表.

When you execute a query against the Firebase Database, there will potentially be multiple results. So the result of the query will be a list of those results. Even if there is only a single result, the it will be a list of one result.

这篇关于Firebase:获取对第一项/元素的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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