Reputation: 37
I want to get the children of "games" but I have to know the names of the children.
public void getdata()
{
FirebaseApp.DefaultInstance.SetEditorDatabaseUrl("url...");
FirebaseDatabase.DefaultInstance.GetReference("games").GetValueAsync().ContinueWith(task => {
if (task.IsFaulted)
{
Debug.Log(task.Exception);
}
else if (task.IsCompleted)
{
DataSnapshot snapshot = task.Result;
//Get the children of "games" and Debug.log them
snapshot.Child("path"); // Requires the path. What if I don't know the path ?
}
});
}
Doesen't there exist something like:
snapshot.Child(0);
snapshot.Child(1);
Upvotes: 0
Views: 693
Reputation: 598728
As Doug said, the Firebase API doesn't allow index-based access to child nodes. You either need to know the key of the child node, or you can iterate over all child nodes.
DataSnapshot snapshot = task.Result;
foreach (DataSnapshot child in snapshot.Children) {
Debug.Log("Received value for leader: "+child.Value);
}
If you want to access a specific property of each child node, you'd do for example child.Child("Score").Value
.
Upvotes: 2
Reputation: 317372
Firebase doesn't offer an array-like view of your data, and there is no default ordering of the children. The order you're seeing in the console is just an alphabetical sort on the name of the children.
You will need to either know the names of the children, or be able to use an ordered type query where the data in the children themselves are used to determine which one comes first. I suggest reading the documentation to learn how to query by ordering. You will need to define some data in your children to figure out which one should come first. Then, to get only the first child, you can limit the results to just one.
Upvotes: 1