Caleb Wolff
Caleb Wolff

Reputation: 65

How to use DataSnapshot with Firebase for Unity?

Can anyone explain why this code never makes it the the Debug.Log("END"); call?

I'm trying to figure out how to use the DataSnapshot for Firebase in Unity but the documentation is next to worthless when it comes to working with the datasnapshot. I am able to get the snapshot but working with it is excessively complicated.

I want to make a single call to the database and get a snapshot of all things under the reference "levels"

The database rules look like:

"rules": {
    "levels" : {
       "levelNumber" : {
             "oneStarTime" : 0,
             "twoStarTime" : 30,
             "threeStarTime" : 45
                       }
               },
     "users" : .....

Here is my method:

void GetLevelSnapshot()
    {
        FirebaseDatabase.DefaultInstance.GetReference("levels").OrderByChild("levelNumber").GetValueAsync().ContinueWith(task => {
            if (task.IsFaulted) 
            {
                // Handle the error...
            }
            else if (task.IsCompleted) 
            {
                levelSnapshot = task.Result;

                foreach(var childSnapshot in levelSnapshot.Children)
                {
                    Debug.Log("BEGIN");
                    //Debug.Log(childSnapshot.Key);
                    LevelList.Add(childSnapshot.Key);
                    Debug.Log("END");
                }

            }
        });
    }

In the end I'm looking for a way to essentially iterate through the snapshot on a specified level and pull all children. However, at this point I have not been able to get childSnapshot.Value to give me anything readable.

Any help on the matter is greatly appreciated!

Upvotes: 3

Views: 5880

Answers (2)

Shariful Islam Mubin
Shariful Islam Mubin

Reputation: 2286

Follow these steps:

User class:


public class User
{
    public string name;
    public string[] items;
    public string email;

    public string ToString()
    {
        return name + ", " + email + ", " + items.ToString();
    }
}

Read data from Firebase Realtime Database:

FirebaseDatabase.DefaultInstance.RootReference.Child("users").Child("FIREBASE_UID_HERE").GetValueAsync().ContinueWith(t =>
{
    if (t.IsCanceled)
    {
        Debug.Log("FirebaseDatabaseError: IsCanceled: " + t.Exception);
        return;
    }

    if(t.IsFaulted)
    {
        Debug.Log("FirebaseDatabaseError: IsFaulted:" + t.Exception);
        return;
    }

    DataSnapshot snapshot = t.Result;
    User user = JsonUtility.FromJson<User>(snapshot.GetRawJsonValue());
    Debug.Log(user.ToString());

});

Upvotes: 4

DcoderZ
DcoderZ

Reputation: 120

    void GetLevelSnapshot()
{
  FirebaseDatabase.DefaultInstance.GetReference("levels").GetValueAsync().ContinueWith(task => {
        if (task.IsFaulted) 
        {
            // Handle the error...
        }
        else if (task.IsCompleted) 
        {
            DataSnapshot levelSnapshot = task.Result;

            foreach(var rules in levelSnapshot.Children) // rules
            {
                  Debug.LogFormat("Key = {0}", rules.Key);  // "Key = rules"
               foreach(var levels in rules.Children)         //levels
                {
                    Debug.LogFormat("Key = {0}", levels.Key); //"Key = levelNumber"
                   foreach(var levelNumber in levels.Child) // levelNumber
                     {
                     //Debug.Log("BEGIN");
                     Debug.LogFormat("Key = {0}, Value = {0}", levelNumber.Key, levelNumber.Value.ToString()); //"oneStarTime" : 0,"twoStarTime" : 30,"threeStarTime" : 45
                     //Debug.Log("END");
                     } // levelNumbers
                }  // levels
            } //rules
        }
    });
}

I hope this helps if you need more explanation please don't hesitate to comment. I didn't actually have time to test it out but I've recently been working extensively with unity3d and firebase. I also removed the OrderByChild() seems that you are wanting to get the levels in order not sure if this will affect the output so you can try it with or without it. Please let me know if it works.

Upvotes: 1

Related Questions