小编Jes*_*ans的帖子

如何以正确的方式在firebase查询上加入结果

我正在使用Firebase实时数据库来构建我的应用程序Unity.为了构建"朋友"排行榜,数据库将跟踪用户的朋友及其分数.

该数据库具有以下结构:

scores{
user id : score
}
Users:{
    Id: {
        ageRange
        email
        name
        friendlist : {
             multiple friends user ids
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是为了获得应用程序必须进行大量api调用的每个朋友的分数和名称.至少如果我正确理解firebase.如果用户有10个朋友,则在填充排行榜之前将进行21次呼叫.

我想出了用c#编写的以下代码:

List<UserScore> leaderBoard = new List<UserScore>();
db.Child("users").Child(uid).Child("friendList").GetValueAsync().ContinueWith(task => {
    if (task.IsCompleted)
    {
        //foreach friend
        foreach(DataSnapshot h in task.Result.Children)
        {
            string curName ="";
            int curScore = 0;
            //get his name in the user table
            db.Child("users").Child(h.Key).GetValueAsync().ContinueWith(t => {
                if (t.IsCompleted)
                {
                    DataSnapshot s = t.Result;
                    curName = s.Child("name").Value.ToString();
                    //get his score from the scores table …
Run Code Online (Sandbox Code Playgroud)

c# unity-game-engine firebase firebase-realtime-database

5
推荐指数
1
解决办法
879
查看次数