Firebase数据库-检查孩子是否存在

时间:2019-03-19 20:49:12

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

我已经检查了类似的主题,但是有使用侦听器的代码,我不确定如何使用它们。您能帮我如何有效地使用它(最少的数据交换)吗?

预先感谢

我的读取数据库代码,但是,如果数据库中不存在子UserID(即首次创建...),我想做些事情。我在上一个ELSE中尝试了一些方法,但是它不起作用:/

public void ReadDatabase()
{
    string uid;

    if (usermanager != null && usermanager.user != null)
    {
        uid = usermanager.user.UserId;
        Debug.Log("DataManager: Set User ID from UserManager for User: " + uid);
    }
    else
    {
        Debug.LogError("DataManager: Set User not set.");
        return;
    }

    FirebaseDatabase.DefaultInstance.GetReference("Players").Child(uid).GetValueAsync().ContinueWith(task =>
    {
        if (task.IsFaulted)
        {
            Debug.LogError("DataManager: read database is faulted with error: " + task.Exception.ToString());
            return;
        }

        if (task.IsCompleted)
        {
            DataSnapshot snapshot = task.Result;
            Debug.Log("DataManager: Read scores");

            Dictionary<string, System.Object> attributes = (Dictionary<string, System.Object>)snapshot.Value;

            if (snapshot.Exists)
            {
                Debug.Log("DataManager: UID ==> " + attributes["uid"].ToString());
                Debug.Log("DataManager: Score ==> " + attributes["score"].ToString());
                Debug.Log("DataManager: Level ==> " + attributes["level"].ToString());
                Debug.Log("DataManager: Kill ==> " + attributes["kill"].ToString());
                Debug.Log("DataManager: Death ==> " + attributes["death"].ToString());
                Debug.Log("DataManager: XP ==> " + attributes["xp"].ToString());
                Debug.Log("DataManager: Live ==> " + attributes["live"].ToString());
            }
            ***else
            {
                Debug.LogError("DataManager: Database for the user not available.");
            }***

        }
    });
}

0 个答案:

没有答案