当我添加EventListener来检查键入的用户电话号码是否存在于Firebase Realtime数据库中的所有电话号码中时,我一直试图在OnDataChange方法内进行循环:
val telephoneInput: String = telephone.text.toString()
var numberExistInDataBase = false
refDataBaseApp.addValueEventListener(object : ValueEventListener {
override fun onCancelled(p0: DatabaseError) {
Log.i(TAG, "Estoy en onCancelled")
}
override fun onDataChange(dataSnapshot: DataSnapshot) {
Log.i(TAG, "Estoy en OnDataChange")
Log.i(TAG, "Outside For el userPhoneInput $telephoneInput")
for (snapshot in dataSnapshot.children) {
//Log.i(TAG,"For snapshot app_user_phone $snapshot")
var userPhoneAppDatabase = snapshot.child("app_user_phone").value
Log.i(TAG, "Snapshot Child userPhoneAppDatabase $userPhoneAppDatabase")
if (telephoneInput == userPhoneAppDatabase) {
Log.i(TAG, "userPhoneInput $telephoneInput")
Log.i(TAG, "userPhoneAppDatabase $userPhoneAppDatabase")
numberExistInDataBase = true
Log.i(TAG, "numberExistInDataBase $numberExistInDataBase")
alertDialogChangeTelephone()
telephone.error = "Votre numéro de téléphone est déjà enregistré"
break
} else {
numberExistInDataBase = false
}
}
if (numberExistInDataBase == true) {
Log.i(TAG, "number exist true = $numberExistInDataBase")
} else{
signUpUser ()
}
我按以下顺序尝试:
first->我键入一个我知道它存在的数字 如果数据库中存在键入的数字,它将返回显示AlertDialog,并且不会执行signUpUser。
第二个->我输入一个我不知道的数字。 问题是当键入的数字在数据库中不存在时。第一个循环表明不存在。但是在以下循环中,它显示AlertDialog(好像它存在)。之后,将创建signUpUser方法。
当我查看日志时,似乎已键入的数字已保存在缓存中,这就是显示AlertDialog的原因。这意味着它将我第一次输入而不是最后一个输入的数字作为inputPhoneNumber。
所以问题是:为什么将输入的第一个数字保存在缓存中?循环发生了什么?非常感谢您的帮助!
日志: 首先->
I/LOGIN_FRAGMENT: Estoy al principio
I/LOGIN_FRAGMENT: Estoy en OnDataChange
I/LOGIN_FRAGMENT: Outside For el userPhoneInput 0616747368
I/LOGIN_FRAGMENT: Snapshot Child userPhoneAppDatabase 0623578561
I/LOGIN_FRAGMENT: Snapshot Child userPhoneAppDatabase 0616747368
I/LOGIN_FRAGMENT: userPhoneInput 0616747368
I/LOGIN_FRAGMENT: userPhoneAppDatabase 0616747368
I/LOGIN_FRAGMENT: numberExistInDataBase true
I/LOGIN_FRAGMENT: aqui viene el alert dialog
I/LOGIN_FRAGMENT: number exist true = true
秒->
I/LOGIN_FRAGMENT: Estoy al principio
I/LOGIN_FRAGMENT: Estoy en OnDataChange
I/LOGIN_FRAGMENT: Outside For el userPhoneInput 0603254542
I/LOGIN_FRAGMENT: Snapshot Child userPhoneAppDatabase 0623578561
I/LOGIN_FRAGMENT: Snapshot Child userPhoneAppDatabase 0616747368
I/LOGIN_FRAGMENT: Snapshot Child userPhoneAppDatabase 0699268704
I/LOGIN_FRAGMENT: number does not exist false = false
I/LOGIN_FRAGMENT: Estoy en OnDataChange
I/LOGIN_FRAGMENT: Outside For el userPhoneInput 0616747368
I/LOGIN_FRAGMENT: Snapshot Child userPhoneAppDatabase 0623578561
I/LOGIN_FRAGMENT: Snapshot Child userPhoneAppDatabase 0616747368
I/LOGIN_FRAGMENT: userPhoneInput 0616747368
I/LOGIN_FRAGMENT: userPhoneAppDatabase 0616747368
I/LOGIN_FRAGMENT: numberExistInDataBase true
I/LOGIN_FRAGMENT: aqui viene el alert dialog
I/LOGIN_FRAGMENT: number exist true = true
I/LOGIN_FRAGMENT: Estoy en OnDataChange
I/LOGIN_FRAGMENT: Outside For el userPhoneInput 0603254542
I/LOGIN_FRAGMENT: Snapshot Child userPhoneAppDatabase 0623578561
I/LOGIN_FRAGMENT: Snapshot Child userPhoneAppDatabase 0616747368
I/LOGIN_FRAGMENT: Snapshot Child userPhoneAppDatabase 0699268704
I/LOGIN_FRAGMENT: Snapshot Child userPhoneAppDatabase 0603254542
I/LOGIN_FRAGMENT: userPhoneInput 0603254542
I/LOGIN_FRAGMENT: userPhoneAppDatabase 0603254542
I/LOGIN_FRAGMENT: numberExistInDataBase true
I/LOGIN_FRAGMENT: aqui viene el alert dialog
I/LOGIN_FRAGMENT: number exist true = true