user13311595
user13311595

Reputation:

Firebase method don't allow a return

I have this method looseMoney(...) with a firebase database. Unfortunately the methods onSuccess() and onFailure() don't allow it to return any value. I want to check if the transaction is succesfull or not? But how could I do that? You can see my code below. What am I missing? I am grateful for every answer. Thank you!

private int looseMoney(String pUserID, final int pAmount) {
    final FirebaseFirestore db = FirebaseFirestore.getInstance();
    final DocumentReference sfDocRefAbs = db.collection("users").document(pUserID);


    db.runTransaction(new Transaction.Function<Void>() {
        @Override
        public Void apply(Transaction transaction) throws FirebaseFirestoreException {

            DocumentSnapshot snapshotAbs = transaction.get(sfDocRefAbs);
            int neuerKontostandAbs = 0;
            if(pAmount <= (snapshotAbs.getDouble("kontostand"))) {
                neuerKontostandAbs = (int) (snapshotAbs.getDouble("kontostand") - pAmount);
                transaction.update(sfDocRefAbs, "kontostand", neuerKontostandAbs);
            }
            else {
                //return 1;
            }

            return null;
        }
    }).addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void aVoid) {
            Toast.makeText(getApplicationContext(), "Kontostand erfolgreich angepasst", Toast.LENGTH_SHORT).show();
            //return 2;
        }
    })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Toast.makeText(getApplicationContext(), "Transaktion fehlgeschlagen: " + e.toString(), Toast.LENGTH_SHORT).show();
                    //return 3;
                }
            });

}

Upvotes: 1

Views: 79

Answers (1)

Raamyy
Raamyy

Reputation: 580

We have faced this case and we figured out a simple solution.

First of all you should know that firebase retrieval functions are asynchronous functions. i.e. you will need a call back to be triggered when firebase does it's job.

We have created a simple interface called RetrievalEventListener which provide functions you can call inside the onSuccess event for example.

public interface class RetrievalEventListener<T> {
    public abstract void OnDataRetrieved(T t);
}

This interface can be passed as a parameter and you call the onDataRetrieved Function when you want to retrieve the value.

private void looseMoney(String pUserID, final int pAmount, RetrievalEventListener<int> retrievalEventListener) {
    final FirebaseFirestore db = FirebaseFirestore.getInstance();
    final DocumentReference sfDocRefAbs = db.collection("users").document(pUserID);


    db.runTransaction(new Transaction.Function<Void>() {
        @Override
        public Void apply(Transaction transaction) throws FirebaseFirestoreException {

            DocumentSnapshot snapshotAbs = transaction.get(sfDocRefAbs);
            int neuerKontostandAbs = 0;
            if(pAmount <= (snapshotAbs.getDouble("kontostand"))) {
                neuerKontostandAbs = (int) (snapshotAbs.getDouble("kontostand") - pAmount);
                transaction.update(sfDocRefAbs, "kontostand", neuerKontostandAbs);
            }
            else {
                //return 1;
                retrievalEventListener.onDataRetrieved(1);
            }

            return null;
        }
    }).addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void aVoid) {
            Toast.makeText(getApplicationContext(), "Kontostand erfolgreich angepasst", Toast.LENGTH_SHORT).show();
            retrievalEventListener.onDataRetrieved(2);
        }
    })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Toast.makeText(getApplicationContext(), "Transaktion fehlgeschlagen: " + e.toString(), Toast.LENGTH_SHORT).show();
                    //return 3;
                    retrievalEventListener.onDataRetrieved(3);
                }
            });

}

How should you call the function?

It should like this:

String pUserID = "someId";
int pAmount = 10;
looseMoney(pUserID, pAmount, new RetrievalEventListener<int>() {
    @Override
    public void OnDataRetrieved(int number) {
    // Now you have the required number do what do you need with it
    }
});

If you want more clarifications let me know :)

Upvotes: 1

Related Questions