Home > Mobile >  Different value from the same child in Firebase Unity
Different value from the same child in Firebase Unity

Time:12-30

When I start game in the Unity Editor, I get the float value from the DB (Realtime DB Firebase) and pring it in the console.

Problem: the value sometimes is different. When in the DB it's 1, I am getting 4 from the DB. I run TryDownloadData method to download the data and print it.

The DB structure is very simple:

{
  "users" : {
    "5bf700187027d43cc8295afb69cc4495aecff695" : {
      "gravitationalAcceleration" : -9.8100004196167
    }
  }
}

User class:

public class User
{
    public float gravitationalAcceleration;

    public User(float gravitationalAcceleration)
    {
        this.gravitationalAcceleration = gravitationalAcceleration;
    }
}

And finally the class that connects to the DB and gets the gravitationalAcceleration value.

public class Firebase : MonoBehaviour
{
    private string userID;

    private DatabaseReference dbReference;

    private void Awake()
    {
        userID = SystemInfo.deviceUniqueIdentifier;
        dbReference = FirebaseDatabase.DefaultInstance.RootReference;
        StartCoroutine(TryDownloadData());
    }

    public void CreateUser()
    {
        User user = new User(Physics.gravity.y);

        string json = JsonUtility.ToJson(user);
        dbReference.Child("users").Child(userID).SetRawJsonValueAsync(json);

        print("New user created");
    }

    public void TrySendData(User user)
    {
        StartCoroutine(SendData(user));
    }

    private IEnumerator SendData(User user)
    {
        string json = JsonUtility.ToJson(user);
        var task = dbReference.Child("users").Child(userID).SetRawJsonValueAsync(json);

        yield return new WaitUntil(() => task.IsCompleted);

        if (task.Exception != null)
        {
            print("Exception uploading data!");
            print(task.Exception);
        }

        print("Data sent!");
    }

    private IEnumerator TryDownloadData()
    {
        var task = dbReference.Child("users").GetValueAsync();

        yield return new WaitUntil(() => task.IsCompleted);

        if (task.Exception != null)
        {
            print("Exception while downloading data!");
            print(task.Exception);
        }    

        if (task.Result.Value == null)
        {
            print("No info from DB");
            CreateUser();
        }
        else
        {
            print("Got Data!");
            DataSnapshot snapshot = task.Result;
            if (snapshot.Child(userID).Exists)
            {
                var val = float.Parse(snapshot.Child(userID).Child("gravitationalAcceleration").Value.ToString());
                Debug.Log("User exists. The value "   val);
            }
            else
            {
                Debug.Log("User does not exist");
                CreateUser();
            }
        }
    }
}

CodePudding user response:

You need to initialize the firebase. After that it started working ok! (returning correct values)

private string userID;
private DatabaseReference dbReference;

    private void Start()
    {
        Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWithOnMainThread(task =>
        {
            if (task.Exception != null)
            {
                Debug.Log("Init Exception! "   task.Exception);
                return;
            }

            userID = SystemInfo.deviceUniqueIdentifier;
            dbReference = FirebaseDatabase.DefaultInstance.RootReference;
            //OnInit?.Invoke();
            Debug.Log("Firebase initialized!");
            TryLogin();
        });
    }
  • Related