[英]Android studio/Firebase - hitting an infinite loop trying to fetch data
我正在嘗試從我的firebaseDB中獲取數據,但它一直在無限地進行,我看不出原因。
問題:用戶鍵入郵件來匯款。 我從數據庫中提取所有用戶,並將寫入的電子郵件與每個用戶的電子郵件進行比較。 如果電子郵件與用戶匹配(電子郵件是唯一的),則在內部進行另一次數據庫調用,獲取收件人的余額。 用戶的余額已更新,收件人也是如此。 問題是它不斷減去發件人的金額,並不斷為收件人添加(通過點擊一次)。
我知道這與異步執行有關,但我對編程很新,不知道如何解決它。
有人可以幫忙嗎? 代碼如下:
public void sendTo(){
final String receiverEmail = editTextReciever.getText().toString().trim();
String amountHolder = ediTextAmount.getText().toString().trim();
if(TextUtils.isEmpty(receiverEmail)){
Toast.makeText(getApplicationContext(), "Enter an email", Toast.LENGTH_LONG).show();
return;
}
if(TextUtils.isEmpty(amountHolder)){
Toast.makeText(getApplicationContext(), "Enter an amount to send", Toast.LENGTH_LONG).show();
return;
}
//If it has gotten this far, theres data in the editText (the editText can only contain numbers)
final int amount = Integer.parseInt(amountHolder);
//Looping through every user in the DB and checking if the typed email belong to any of the users in the DB
FirebaseDatabase.getInstance().getReference("users").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
User user = snapshot.getValue(User.class);
//If typed email matches a user in the db (emails are unique due to firebaseAuthentication), continue
if(user.getEmail().equals(receiverEmail)){
recipientUid = snapshot.getKey();
if(accountBalance - amount >= 0 && uid != snapshot.getKey()){
//I have to fetch our the balance of the user i'm sending money to so I can add the amount to his balance
FirebaseDatabase.getInstance().getReference("Accounts/" + accountType).child(snapshot.getKey()).child("balance").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
int balanceOfReceiver = Integer.parseInt(dataSnapshot.getValue().toString());
int updatedBalance = balanceOfReceiver + amount;
//The senders balance is updated
accountBalance = accountBalance - amount;
FirebaseDatabase.getInstance().getReference("Accounts/" + accountType).child(uid).child("balance").setValue(accountBalance);
//And the recipients balance is updated
FirebaseDatabase.getInstance().getReference("Accounts/" + accountType).child(recipientUid).child("balance").setValue(updatedBalance);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
} else {
Toast.makeText(getApplicationContext(), "You can't overdraw or send money to yourself", Toast.LENGTH_LONG).show();
}
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
您將ValueEventListener添加到用戶ref,並在數據更改時調用onDataChange。 你減去金額,再次觸發onDataChange然后你進入一個循環。
您需要重新考慮代碼結構或在更改金額之前刪除偵聽器。
這個
FirebaseDatabase.getInstance().getReference("Accounts/" + accountType).child(uid).child("balance").setValue(accountBalance);
FirebaseDatabase.getInstance().getReference("Accounts/" + accountType).child(recipientUid).child("balance").setValue(updatedBalance);
觸發這個
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.