如何在更新前检查Firebase数据库中的值

时间:2019-02-20 19:50:12

标签: android firebase firebase-realtime-database

我有以下代码来更新Firebase数据库中的问题分数:

用于将分数发送到Firebase的代码:

setAuthStateListener(new FirebaseAuth.AuthStateListener() {
                @Override
                public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                    FirebaseUser user = firebaseAuth.getCurrentUser();
                        if (user != null) {
                        String username = mAuth.getCurrentUser().getDisplayName();
                        //Upload points to Database
                        question_score.child(String.format("%s_%s", username,
                                Common.categoryID))
                                .setValue(new QuestionScore(String.format("%s_%s", username,
                                        Common.categoryID),
                                        username,
                                        String.valueOf(score),
                                        Common.categoryID,
                                        Common.categoryName));

                    }


                    // Log.d(LOGIN_STATUS_CHANGED_TAG, "onAuthStateChanged:signed_in:" + user.getUid());


                    else {
                        //  Log.d(LOGIN_STATUS_CHANGED_TAG, "onAuthStateChanged:signed_out");
                        // close the game room
                        finish();
                    }
                }
            });

        }

问题分数模型

public QuestionScore(String question_Score, String user, String score, String categoryID, String categoryName) {
        Question_Score = question_Score;
        this.user = user;
        this.score = score;
        CategoryID = categoryID;
        CategoryName = categoryName;
    }

和Firebase json:

"Smrodo_01" : {
      "categoryID" : "01",
      "categoryName" : "Intelligence",
      "question_Score" : "Smrodo_01",
      "score" : "30",
      "user" : "Smrodo"
    },

如何检查现有分数的值并仅在新分数高于现有分数时更新?

评论是否需要澄清。

0 个答案:

没有答案