如何从以DocumentReference为参数的方法中获得双倍回报?

时间:2018-12-22 14:20:16

标签: java android firebase google-cloud-firestore

因此,我在Firestore中创建了一个简单的collection(Wallet),该collection具有包含两个字段(balance,timestamp)的文档。我创建了一个double方法,该方法以DocumentReference作为参数并返回wallet的外观,也是一个内部对象WalletModel,它检索快照并从我的自定义模型获取

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;
}

我在onSucces方法中得到了想要的结果,但最后得到了0的返回值。但是,我希望从firabase获取数据(账单)并将其用于makeTransaction方法。

0 个答案:

没有答案