Работа с транзакциями и массивами с базой данных Firebase - PullRequest
0 голосов
/ 15 декабря 2018

Я сохраняю данные таблицы лидеров, используя , сохраняю данные как транзакции

Я ищу способ удалить нумерацию массивов транзакциями Firebase и заменить их на userId, чтобы я былвозможность обновлять информацию о пользователе.Код:

 private TransactionResult AddScoreTransaction(MutableData mutableData)
    {
        playerNewGlobalScore = false;
        List<object> leaders = mutableData.Value as List<object>;
        if (leaders == null)
        {
            leaders = new List<object>();
        }
        else if (mutableData.ChildrenCount >= LeaderBoardManager.Instance.MaxScoreRows)
        {
            // If the current list of scores is greater or equal to our maximum allowed number,
            // we see if the new score should be added and remove the lowest existing score.
            long minScore = long.MaxValue;
            object minVal = null;

            foreach (var child in leaders)
            {
                if (!(child is Dictionary<string, object>))
                    continue;
                long childScore = (long)((Dictionary<string, object>)child)["score"];
                if (childScore < minScore)
                {
                    minScore = childScore;
                    minVal = child;
                }
            }
            // If the new score is lower than the current minimum, we abort.
            if (minScore > Score)
            {
                return TransactionResult.Abort();
            }
            // Otherwise, we remove the current lowest to be replaced with the new score.

            leaders.Remove(minVal);
        }

        // Now we add the new score as a new entry that contains the email address and score.
        Dictionary<string, object> newScoreMap = new Dictionary<string, object>();

        newScoreMap["name"] = Name;
        newScoreMap["country"] = Country;
        newScoreMap["photoUrl"] = PhotoUrl;
        newScoreMap["level"] = Level;
        newScoreMap["userId"] = UserId;
        newScoreMap["score"] = Score;

        leaders.Add(newScoreMap);

        // You must set the Value to indicate data at that location has changed.
        mutableData.Value = leaders;

        playerNewGlobalScore = true;
        return TransactionResult.Success(mutableData);
    }

Пример базы данных в реальном времени: enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...