How to retrieve data from Firebase after Authentication in Unity?

时间:2019-04-23 15:20:13

标签: c# firebase unity3d firebase-realtime-database firebase-authentication

Hello guys I'm new to Firebase and I am trying to develop a simple bar chart data visualization that connected to the realtime database of firebase. I am trying to read/retrieve data from firebase after authentication. First, when I tried to retrieve the data from firebase without authentication, it works fine in my Unity Editor. But when I setup the authentication, it can't read the method "HandleValueChanged" which is to read/retrieve data from firebase. The HandleValueChanged is a method calling from the "FirebaseDatabase.DefaultInstance.GetReference("Node1").ValueChanged += HandleValueChanged". How can I fix this issue or is there any other ways to retrieve data from firebase after the auth.SignInWithEmailAndPasswordAsync(email, password)...etc.?

My security rules

{

  "rules": {
        ".read": "auth != null",
        ".write": "auth != null"
  }
}

This is my Start Method()

public void Start()
    {

Firebase.FirebaseApp.DefaultInstance.SetEditorP12FileName("xxxxxxxxxxx.p12");
        Firebase.FirebaseApp.DefaultInstance.SetEditorServiceAccountEmail("xxxxxxxxxxxxxxxxxx.iam.gserviceaccount.com");
        Firebase.FirebaseApp.DefaultInstance.SetEditorP12Password("notasecret");


        Firebase.DependencyStatus dependencyStatus = Firebase.DependencyStatus.UnavailableOther;
        Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task => {
            dependencyStatus = Firebase.FirebaseApp.CheckDependencies();
            //dependencyStatus = task.Result;
            if (dependencyStatus == Firebase.DependencyStatus.Available)
            {
                Debug.Log("Successfully It will now Initialize Firebase " + dependencyStatus);
                InitializeFirebase();
            }
            else
            {
                Debug.LogError(
                  "Could not resolve all Firebase dependencies: {0}" + dependencyStatus);
            }
        });
    }

After Checking the Dependency status, it will now initialize the firebase. Here's my InitializeFirebase() method:

public void InitializeFirebase()
    {
        Firebase.FirebaseApp app = FirebaseApp.DefaultInstance;
        app.SetEditorDatabaseUrl("https:xxxxxxxxxxxxxxxxxxxxxx.com");

        if (app.Options.DatabaseUrl != null)
        {
            app.SetEditorDatabaseUrl(app.Options.DatabaseUrl);
        }

        auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
        auth.SignInWithEmailAndPasswordAsync("email@mail.com", "password").ContinueWith(task =>
        {

            if (task.IsCanceled)
            {
                Debug.LogError("SignInWithEmailAndPasswordAsync was canceled.");
                return;
            }
            if (task.IsFaulted)
            {
                Debug.LogError("SignInWithEmailAndPasswordAsync encountered an error: " + task.Exception);
                return;
            }

            Firebase.Auth.FirebaseUser newUser = task.Result;
            Debug.LogFormat("User signed in Successfully: {0} ({1})", newUser.DisplayName, newUser.UserId);

            GetData();

        });
    }

And now when it reach the GetData method, I write a Debug.Log to trace the process and to see if it enters to this method after the GetReference.

public void GetData()
    {

        FirebaseDatabase.DefaultInstance.GetReference("Node1").ValueChanged += HandleValueChanged;

        Debug.Log("1");
}

Now as a result, the Debug.Log("1") displayed on the Unity Console but the Debug.Log("2") inside of the "HandleValueChanged" method didn't display. This is the part where it now retrieving the data from the realtime database of firebase but there's no output when I run my Unity Editor.

public void HandleValueChanged(object sender, ValueChangedEventArgs args)
    {
        Debug.Log("2");

      if (args.DatabaseError != null)
        {
            Debug.LogError(args.DatabaseError.Message);
            return;
        }

        Debug.Log(args.Snapshot.Child("Node2").Value);
        NumOfOrdersFriedChicken = args.Snapshot.Child("NumberOfOrders").Value.ToString();
        NumOfOrdersLemonGrass = args.Snapshot.Child("NumberOfOrdersLemonGrass").Value.ToString();

        NumOfOrderFriedChickenInt = Int32.Parse(NumOfOrdersFriedChicken);
        NumOfOrderLemonGrassInt = Int32.Parse(NumOfOrdersLemonGrass);

        chart.DataSource.SetValue("Category 1", "Group 1", NumOfOrderFriedChickenInt);
        chart.DataSource.SetValue("Category 2", "Group 1", NumOfOrderLemonGrassInt);
        //chart.DataSource.SlideValue("Category 2", "Group 1", NumOfOrderLemonGrassInt, 4f);

    }

I only expect an output of a 3D Bar Chart for Data Visualization that connected to the firebase. How can I retrieve my data after authentication?

0 个答案:

没有答案