当前位置: 动力学知识库 > 问答 > 编程问答 >

android - ValueEventListener() is not reading data before data is updated

问题描述:

I am building an android app and using firebase to store data. My app has classes "Course" and "Quiz".I want to increment the value of quizCount in course object whenever user adds a new Quiz.

This is how they are stored in firebase.

This is the part of code where I am writing to database.

 toIncrementQuizCount = FirebaseDatabase.getInstance().getReference().child(Constants.FIREBASE_COURSES).child(userId).child(courseKey);

toIncrementQuizCount.addValueEventListener(new ValueEventListener() {

@Override

public void onDataChange(DataSnapshot dataSnapshot) {

Course course = dataSnapshot.getValue(Course.class);

if(course != null) {

// int quizCount is intially set to 0

quizCount = course.getQuizCount();

// Toast 1

Toast.makeText(getActivity(), "inside listener " + Integer.toString(quizCount), Toast.LENGTH_SHORT).show();

}

}

@Override

public void onCancelled(DatabaseError databaseError) {

}

});

Quiz quiz = new Quiz(quizTitle, scheduledDate,marksPerQs, 0);

mDatabase.child(Constants.FIREBASE_QUIZZES).child(courseKey).push().setValue(quiz);

// Toast 2

Toast.makeText(getActivity(), Integer.toString(quizCount), Toast.LENGTH_SHORT).show();

quizCount = quizCount + 1;

// Toast 3

Toast.makeText(getActivity(),"after increment " + Integer.toString(quizCount), Toast.LENGTH_SHORT).show();

mDatabase.child(Constants.FIREBASE_COURSES).child(userId).child(courseKey).child(Constants.FIREBASE_COURSES_QUIZCOUNT).setValue(quizCount);

Now the problem is that quiz is successfully stored in database but the value of quizCount is not incremented.

Toast 2 and Toast 3 appears immediately after quiz is added and after quizCount is updated in Firebase by last line of code(always updated to 1 since quizCount variable is initially 0 and then 1 after increment) then Toast 1 appears.This signifies that ValueEventListener() is called when data is changed in database. I dont want this.I want to read value from database to increment it.

I have also tried AddListenerForSingleValueEvent() but then Toast 1 didn't appear at all.

Kindly tell me how to read the value of quizCount so that I increment it and update it in database.

Much thanks

网友答案:

I don't know if this is the right way but I worked around.I am still looking for a better way but posting my answer for someone looking for a quick fix. Here is what I did to increment the value in database.I had initially put

count = course.getQuizCount();
 count++;
                         mDatabase.child(Constants.FIREBASE_COURSES).child(userId).child(courseKey).child(Constants.FIREBASE_COURSES_QUIZCOUNT).setValue(count);

inside my ValueEventListener() but that brought a dramatic increment in quizCount, the value crossed hundred. Now I declared a boolean variable isIncremented and rest is explained in code.

// this method is called when user clicks on "add" button to add quiz     
public void addQuiz() {
            isIncremented = false;
            final String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();

        // some irrelevant code here
        else{
            toIncrementQuizCount.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    Course course     = dataSnapshot.getValue(Course.class);
                    if(course != null && isIncremented == false) {
                        count = course.getQuizCount();
                        count++;
                        mDatabase.child(Constants.FIREBASE_COURSES).child(userId).child(courseKey).child(Constants.FIREBASE_COURSES_QUIZCOUNT).setValue(count);
                   isIncremented = true;
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
            Quiz quiz = new Quiz(quizTitle, scheduledDate,marksPerQs,  0);
            mDatabase.child(Constants.FIREBASE_QUIZZES).child(courseKey).push().setValue(quiz);
            Toast.makeText(getActivity(), Integer.toString(count), Toast.LENGTH_SHORT).show();

            count = count + 1;
            Toast.makeText(getActivity(),"after increment " + Integer.toString(count), Toast.LENGTH_SHORT).show();

            mDatabase.child(Constants.FIREBASE_COURSES).child(userId).child(courseKey).child(Constants.FIREBASE_COURSES_QUIZCOUNT).setValue(count);

        }
        }

This worked for me.

分享给朋友:
您可能感兴趣的文章:
随机阅读: