본문 바로가기

Android

Firebase 중복키 방지

반응형

mFirebaseDatabase.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(final DataSnapshot dataSnapshot) { for (DataSnapshot data : dataSnapshot.getChildren()) { //If email exists then toast shows else store the data on new key if (!data.getValue(User.class).getEmail().equals(email)) { mFirebaseDatabase.child(mFirebaseDatabase.push().getKey()).setValue(new User(name, email)); } else { Toast.makeText(ChatListActivity.this, "E-mail already exists.", Toast.LENGTH_SHORT).show(); } } } @Override public void onCancelled(final DatabaseError databaseError) { } });

반응형