c#/ Unity - Save value of a Child item in variable

1,684 views
Skip to first unread message

Cruzador

unread,
Mar 15, 2017, 12:16:56 PM3/15/17
to Firebase Google Group
Hello,

I hope no one minds to bother you with a probably super easy question.

After I've set up a firebase Database I'd like to know how I can get the Value of a Childs value. 

        MA1Sensor1.text = FirebaseDatabase
            .DefaultInstance
            .GetReference ("MA1")
            .Child ("Zaehler")
            .GetValueAsync ()
            //.Result
            .ToString ();
        Debug.Log (MA1Sensor1.text);

But unfortunately it only displays: System.Threading.Task.Task1 [Firebase.Database.Datasnapshot]

At the the UI Text within my Unity project is supposed to update whenever there is a change within the database.

 If I display the value of .GetRawJsonValue() in the UI Text that seems already quite good. Changes in the Database will be done immediately in the unity project. Couldn't I just take a part of that Json Value? Or is there a command to get the value of a specific item/ child?

Right now it displays the following RawJson:

{"Program":1,"Zaehler":600}

Theoretically,  transferring the value from "Program"s Childvalue into variable A and "Zaehler"s Childvalue into variable B should be all I need. Somehow I don't know how.

I would highly appreciate any help

THANKS A LOT 

Benjamin Wulfe

unread,
Mar 15, 2017, 2:00:26 PM3/15/17
to Firebase Google Group
Hi Cruzador,

The Firebase SDK uses Tasks to return values to you asynchronously.
Check out this link on how to retrieve a value.

You can also use Unity's "WaitUntil" if you are inside a Unity coroutine to make it simpler.  Note that you can use DataSnapShot.Value to get a parsed value or GetRawJsonValue() if you wanted to see the underlying Json.  In most simple scenarios, you'd want Value.  You'd use "GetRawJsonValue()" if you were using typed objects and Unity's Json serialization.

var getTask = FirebaseDatabase

   
.DefaultInstance
   
.GetReference ("MA1")

   
.Child ("Zaehler") .GetValueAsync();

yield return new WaitUntil(() => getTask.IsCompleted || getTask.IsFaulted);

if (getTask.IsCompleted) {
 
Debug.Log(getTask.Result.Value.ToString());
}

Kato Richardson

unread,
Mar 15, 2017, 2:00:44 PM3/15/17
to Firebase Google Group
Hi Cruzador,

Looks like we could do a bit better at describing this in the guide on reading data. The key here being the `// Do something with the data in args.Snapshot` comment.

You'd use that to access getValue or the enumerable .Children I think.

☼, Kato

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-talk+unsubscribe@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/52ae1835-8738-4bdc-be26-3412569643c6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Kato Richardson | Developer Programs Eng | kato...@google.com | 775-235-8398

Cruzador

unread,
Mar 16, 2017, 11:04:38 AM3/16/17
to Firebase Google Group
Hey Benjamin, thank you very much for your answer.

Until I now tried to implement your mentioned code but it doesn't work. Before showing my code some more information about my goal.
I have two sensors which shall save their values every second (not done yet) in JSON format. With the values having stored in the firebase database including a timestamp I'd like to get the real time value of that sensors. Those are at the end used in a compiled iOS app made with unity.

As I understand 
yield return new WaitUntil(() => getTask.IsCompleted || getTask.IsFaulted);
I need to put in a IEnumerator and later use StartCoroutine within my function.
Thats what I tried to do, but no unity crashes all the time I want to test it... I tried and tried but didn't manage to get it working. May you give me some help with that? Thank you very much.

Now the Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Firebase;
using Firebase.Database;
using Firebase.Unity.Editor;

public class DBManager: MonoBehaviour {

    public Text MA1Sensor1;
    public Text MA1Sensor2;

    void Start() {

        // Set up the Editor before calling into the realtime database.
        FirebaseApp.DefaultInstance.SetEditorDatabaseUrl("https://unityar-6eb13.firebaseio.com/");

        // Get the root reference location of the database.
        DatabaseReference reference = FirebaseDatabase.DefaultInstance.RootReference;

        reference.ValueChanged += HandleValueChanged;
    }

    void HandleValueChanged(object sender, ValueChangedEventArgs args) {

        if (args.DatabaseError != null) {
            Debug.LogError(args.DatabaseError.Message);
            return;
        }
        // DO SOMETHING WITH THE DATA IN ARGS.SNAPSHOT
        var getTask = FirebaseDatabase.DefaultInstance.GetReference ("MA1").Child ("Zaehler").GetValueAsync ();

        StartCoroutine (ReadyData ());

        if (getTask.IsCompleted) {
            Debug.Log(getTask.Result.Value.ToString());
        } else
            Debug.Log ("If Error");
    }

    IEnumerator ReadyData()  {
        var getTask = FirebaseDatabase.DefaultInstance.GetReference ("MA1").Child ("Zaehler").GetValueAsync ();
        yield return new WaitUntil (() => getTask.IsCompleted);
    }

// Update is called once per frame

    void Update ()
    {

    }
}
 


Cruzador

unread,
Mar 16, 2017, 11:04:55 AM3/16/17
to Firebase Google Group
Oh, please ignore my last post. I read yours 10 times, but after my last post I just really got. I made it much more complicated then it was. Just exactly like you said INSIDE a coroutine. 

Thank you very much!


Am Mittwoch, 15. März 2017 19:00:26 UTC+1 schrieb Benjamin Wulfe:
Reply all
Reply to author
Forward
0 new messages