Reputation: 41
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
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;
}
}
});
}
}
Upvotes: 1
Views: 240
Reputation: 80944
Instead of :
mDatabase.addValueEventListener(new ValueEventListener() {
Use the following:
mDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
This way you will retrieve data only once.
Upvotes: 1