【问题标题】:Different value from the same child in Firebase UnityFirebase Unity 中同一个孩子的不同值
【发布时间】:2021-12-27 16:57:52
【问题描述】:

当我在 Unity 编辑器中启动游戏时,我从 DB (Realtime DB Firebase) 中获取浮点值并将其发送到控制台中。

问题:有时值不同。当在数据库中是 1 时,我从数据库中得到 4。我运行TryDownloadData方法下载数据并打印出来。

DB结构很简单:

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

用户类别:

public class User
{
    public float gravitationalAcceleration;

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

最后是连接到数据库并获取gravitationalAcceleration 值的类。

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();
            }
        }
    }
}

【问题讨论】:

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


    【解决方案1】:

    您需要初始化 Firebase。之后它开始正常工作! (返回正确的值)

    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();
            });
        }
    

    【讨论】:

      猜你喜欢
      • 2021-03-11
      • 1970-01-01
      • 1970-01-01
      • 2020-11-14
      • 2019-04-15
      • 2018-05-21
      • 1970-01-01
      • 1970-01-01
      • 2020-11-09
      相关资源
      最近更新 更多