1

I have made a query to retrieve specific data from my firebase. In this case its users score. How can I update the same value I have just retrieved?

Thanks for any help

enter image description here

//check the leaders and their scores, get the score of the current user
    DatabaseReference leadersRef = FirebaseDatabase.getInstance().getReference("Leaders");
    Query query = leadersRef.orderByChild("uID").equalTo(mCurrentUser.getUid());
    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {

            if(snapshot.exists()){
                    for (DataSnapshot child: snapshot.getChildren()) {
                        currentScore = child.child("Score").getValue(Integer.class);
                        Toast.makeText(RunningChallengeActivity.this,"Your current score: " + currentScore,Toast.LENGTH_LONG).show();
                    }
                }
                else {

                //if user has no points create new leader child and set score to 0
                final DatabaseReference newResult = leaderBoard.push();
                String username = mCurrentUser.getEmail();

                //trim email leaving just username
                int index = username.indexOf('@');
                username = username.substring(0,index);

                newResult.child("UserName").setValue(username);
                newResult.child("uID").setValue(mAuth.getCurrentUser().getUid());
                newResult.child("Score").setValue(0);

                }
            }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });

2 Answers 2

2
//check the leaders and their scores, get the score of the current user
    final DatabaseReference leadersRef = FirebaseDatabase.getInstance().getReference("Leaders");
    final Query query = leadersRef.orderByChild("uID").equalTo(mCurrentUser.getUid());
    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {

            if(snapshot.exists()){
                for (DataSnapshot child: snapshot.getChildren()) {

                    //get the key of the child node that has to be updated
                    String postkey = child.getRef().getKey();

                    //update score
                    int new_score = currentScore + score;
                    leadersRef.child(postkey).child("Score").setValue(new_score);
                    Toast.makeText(RunningChallengeActivity.this,"Your current score: " + new_score +"",Toast.LENGTH_LONG).show();

                }
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
Sign up to request clarification or add additional context in comments.

Comments

1

Something like this:

DatabaseReference leadersRef = FirebaseDatabase.getInstance().getReference("Leaders");
Query query = leadersRef.orderByChild("uID").equalTo(mCurrentUser.getUid());
query.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot snapshot) {
        for (DataSnapshot child: snapshot.getChildren()) {
            currentScore = child.child("Score").getValue(Integer.class);
            child.getRef().child("Score").setValue(42);
        }
    }

If you want to set the new value of the score based on its current value, you'll want to use a database transaction instead.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.