【问题标题】:Firebase Retrieve Data - AndroidFirebase 检索数据 - Android
【发布时间】:2019-03-20 04:43:59
【问题描述】:

我可以使用 Unity 将数据写入我的数据库。但是,我想更新现有数据,我该怎么做? (即读取每个现有的子数据并更新它,例如 score_new = score_old + X)我需要使用快照但我无法成功:/

我的数据写入代码:

public void WriteDatabase(int score, int level, int kill, int death, int xp, int live)
{
    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;
    }

    LeaderBoardEntry entry = new LeaderBoardEntry(uid, score, level, kill, death, xp, live);
    Dictionary<string, System.Object> entryValues = entry.ToDictionary();

    Dictionary<string, System.Object> childUpdates = new Dictionary<string, System.Object>();
    childUpdates["/Players/" + uid + "/"] = entryValues;

    reference.UpdateChildrenAsync(childUpdates).ContinueWith(task =>
    {
        if (task.IsCanceled)
        {
            Debug.LogError("DataManager: UpdateChildrenAsync is Cancelled.");
            return;
        }

        if (task.IsFaulted)
        {
            Debug.LogError("DataManager: UpdateChildrenAsync is Faulted.");
            return;
        }

        Debug.Log("DataManager: UpdateChildrenAsync is Completed.");
    });
}

提前致谢,

【问题讨论】:

    标签: c# unity3d firebase-realtime-database


    【解决方案1】:

    我已经用下面的代码解决了这个问题(读取数据库)。对于可能有相同或相似问题的人。

    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.");
                }
    
            }
        });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-05
      • 2019-01-12
      • 1970-01-01
      • 2018-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多