1

I'm using firebase authentication with email and password.

So I want to save user data in realtime database . But when i push the data to database, infinite loop was executing.

I don't know why.. I want to save data like this form and continue saving.

But when I starting, infinite loop is starting

enter image description here

       private void createUser(String email, String password,String name, String phone, String 
   nickname) {
    firebaseAuth.createUserWithEmailAndPassword(email, password)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {


                    if (task.isSuccessful()) {    //Join Success


                        Toast.makeText(JoinActivity.this, "Join Success", Toast.LENGTH_SHORT).show();
                        mDatabase.addValueEventListener(new ValueEventListener() {

                            @Override
                            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                                if(firebaseAuth.getCurrentUser()!=null) {
                                String email=editTextEmail.getText().toString().trim();
                                FirebaseUser user=firebaseAuth.getCurrentUser();

                                String uid=user.getUid();
                                if(dataSnapshot.child("USER").child("user_info").child(uid).exists()){
                                    Toast.makeText(JoinActivity.this,"already exist",Toast.LENGTH_SHORT).show();
                                }else{


                                                   User user_info = new User(uid, editTextId.getText().toString(), editTextEmail.getText().toString(), editTextPassword.getText().toString(),
                                                           editTextname.getText().toString(), editTextNickname.getText().toString(), editTextPhone.getText().toString());

                                                   //delete all data
                                                    mDatabase.setValue(null);
                                                   String key = mDatabase.child("USER").push().getKey();
                                                 //  mDatabase.child("USER").child("user_info").child(key).setValue(user_info);
                                                   Toast.makeText(JoinActivity.this, "save the data", Toast.LENGTH_SHORT).show();
                                               }
                                }

                            }

                            @Override
                            public void onCancelled(@NonNull DatabaseError databaseError) {

                            }
                        });
                        Intent intent = new Intent(JoinActivity.this, MainActivity.class);
                        startActivity(intent);

                    } else {  //already exists
                        Toast.makeText(JoinActivity.this, "already exists", Toast.LENGTH_SHORT).show();
                        return;
                    }
                }

            });
}

}

2
  • have you tried the answer? Commented May 28, 2020 at 13:31
  • Yes... Thank you very much.. Commented May 28, 2020 at 13:45

1 Answer 1

1

Instead of :

mDatabase.addValueEventListener(new ValueEventListener() {

Use the following:

mDatabase.addListenerForSingleValueEvent(new ValueEventListener() {

This way you will retrieve data only once.

Sign up to request clarification or add additional context in comments.

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.