[英]Firebase method don't allow a return
I have this method looseMoney(...)
with a firebase database.我有这个方法和looseMoney(...)
数据库有这种方法。 Unfortunately the methods onSuccess()
and onFailure()
don't allow it to return any value.不幸的是onSuccess()
和onFailure()
方法不允许它返回任何值。 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;
}
});
}
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.首先你应该知道firebase检索函数是异步函数。 ie you will need a call back to be triggered when firebase does it's job.即,当 firebase 完成工作时,您将需要触发回调。
We have created a simple interface called RetrievalEventListener
which provide functions you can call inside the onSuccess event for example.我们创建了一个名为RetrievalEventListener
的简单接口,它提供了可以在 onSuccess 事件中调用的函数。
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.此接口可以作为参数传递,当您要检索值时,您可以调用 onDataRetrieved Function。
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?您应该如何称呼 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:)如果您需要更多说明,请告诉我:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.