我只想一次更新计数表,但是它会不断循环更新,有人可以帮助我。
ionic
答案 0 :(得分:1)
也许,您可以做这样的事情,虽然不是什么都完美,但是它给您一个很好的主意。
oldCount->您可以从document.getData()读取它
var docRef = db.collection(REFERRAL_CODE_KEY).doc(referral_code);
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
Log.d(TAG, "DocumentSnapshot data: " + document.getData());
//read oldCount here from document.getData()
Map<String, Object> docData= new HashMap<>();
docData.put(REFERRAL_COUNT, oldcount + 1);
docRef.set(docData)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d(TAG, "DocumentSnapshot successfully written!");
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w(TAG, "Error writing document", e);
}
});
} else {
Log.d(TAG, "No such document");
}
} else {
Log.d(TAG, "get failed with ", task.getException());
}
}
});
答案 1 :(得分:0)
您可以分离侦听器,以使回调不再被调用。 根据官方文件https://firebase.google.com/docs/firestore/query-data/listen#detach_a_listener,您需要使用remove方法来分离侦听器
Query query = db.collection("cities");
ListenerRegistration registration = query.addSnapshotListener(
new EventListener<QuerySnapshot>() {
// ...
});
// ...
// Stop listening to changes
registration.remove();