简体   繁体   中英

Android studio/Firebase - hitting an infinite loop trying to fetch data

I'm trying to fetch data from my firebaseDB but it keeps going infinitely and I can't see why.

The problem: User types a mail to send money to. I pull out all the users from the db and compare the written email to the email of each user. If the email matches an user (the emails are unique), another db call is made inside, getting the balance of the recipient. The balance of the user is updated and so is it for the recipient. The problem is that it keeps subtracting the amount for the sender and keeps adding for the recipient infinitely (by clicking once).

I know this has something to do with it being executed asynchronous, but I'm too new to programming to know how to solve it.

Can anyone help? Code below:

    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) {

        }
    });
}

You add ValueEventListener to the users ref and onDataChange is called when ever data changes. You subtract the money amount and by that triggering the onDataChange again and you get in to a loop.

you need to rethink your code structure or remove the listener before you change the money amount.

this

FirebaseDatabase.getInstance().getReference("Accounts/" + accountType).child(uid).child("balance").setValue(accountBalance);
FirebaseDatabase.getInstance().getReference("Accounts/" + accountType).child(recipientUid).child("balance").setValue(updatedBalance);

trigger this

public void onDataChange(@NonNull DataSnapshot dataSnapshot) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM