1

So i created a simple collection(Wallet) in firestore that has a document with two fields (balance, timestamp). I created a double method that takes as a parameter a DocumentReference and returns bilance of wallet, also is an inner object WalletModel that retrieves a snapshot and gets from my custom model

public void makeTransaction(View view) {
    String id_credit_receiver = id_credit.getText().toString().toLowerCase();
    double sendingValue = Double.parseDouble(value.getText().toString());


        WriteBatch makeTransaction = db.batch();
        final TransactionModel sendTransaction = new TransactionModel(date, id_credit_receiver, sendingValue, timestamp);
        String updateBilanceOfReceiver = "wallet/" + id_credit_receiver;
        String saveTransactionToReceiver = "transactions/" + id_credit_receiver ;

        DocumentReference receiverBalanceRef = db.document(updateBilanceOfReceiver);
        CollectionReference receiverTransactionRef = db.collection(saveTransactionToReceiver);

        String getBilanceOfReceiverPath =  "wallet/" + id_credit_receiver;
        DocumentReference getReceiverBalanceRef = db.document(getBilanceOfReceiver);
        double getBilance = getReceiversBalance(getReceiverBalanceRef);


        double finalBalanceReceiver = getBilance + sendingValue;
        Map<String, Object> receiverBalance = new HashMap<>();
        receiverBalance.put(KEY_BALANCE, finalBalanceReceiver);


        makeTransaction.set(receiverTransactionRef.document(), sendTransaction);
        makeTransaction.update(receiverBalanceRef, receiverBalance);


        makeTransaction.commit().addOnCompleteListener(this, new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                Toast.makeText(SendActivity.this, "Transaction is made succesfully!", Toast.LENGTH_SHORT).show();

            }
        }).addOnFailureListener(this, new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Toast.makeText(SendActivity.this, "Something went wrong ! " +e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}

public double getReceiversBalance(DocumentReference documentReference){

    documentReference.get().addOnSuccessListener(this, new OnSuccessListener<DocumentSnapshot>() {
        @Override
        public void onSuccess(DocumentSnapshot documentSnapshot) {
            WalletModel walletModel = documentSnapshot.toObject(WalletModel.class);
            balanceReceiver = walletModel.getBalance();

            Log.e("BALANCE", String.valueOf(balanceReceiver) );
        }
    });

    return balanceReceiver;
}

I get the result i want inside onSucces method but i get a 0 return in the end. However, i wish to get the data (bilance) from firabase and use it to makeTransaction method.

Limon
  • 11
  • 2
  • Data is loaded from Firestore asynchronously. This means that your `return balanceReceiver` runs before the `onSuccess`, and thus can't return the correct value yet. You'll either need to put all code that needs the balance into the `onSuccess` method, or pass in an interface that you call from `onSuccess` (similar to `OnSuccessListener`, but then for your case). For more on this, see https://stackoverflow.com/questions/50434836/getcontactsfromfirebase-method-return-an-empty-list/50435519#50435519 – Frank van Puffelen Dec 22 '18 at 14:37

0 Answers0