frankelot
frankelot

Reputation: 14439

Firebase: Get reference to first item/element

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

In my case it would be "ElmmycgjS1Nvq.../albums/-IrdMMds"

enter image description here

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

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

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.

Upvotes: 3

Views: 4461

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599686

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.

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.

Upvotes: 1

Related Questions