【发布时间】:2021-07-02 10:34:00
【问题描述】:
我正在开发一个电子商务应用程序,该应用程序将数据添加到 Firestore,并在另一个活动中获取所有数据并显示,但问题是该方法(用于添加数据)有时运行异步,有时运行两次,有时运行 4 次在某些集合的数据库中,有很多数据和一些数据,但我只需要一个。我不知道我现在能做什么。请帮忙 代码:-
FirebaseFirestore.getInstance().collection(Statics.COLLECTION_NAME)
.document(FirebaseAuth.getInstance().getCurrentUser().getUid())
.collection(Statics.PRODUCT_COLLECTION_NAME)
.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot value, @Nullable FirebaseFirestoreException error) {
boolean isDocPresent = false;
if (value != null && !value.getDocuments().isEmpty()){
List<DocumentSnapshot> snapshots = value.getDocuments();
for (int i = 0; i < snapshots.size(); i++){
String catName = snapshots.get(i).getString(Statics.CATEGORY_FIELD_IN_FIRESTORE);
if (catName.equals(categoryReceive)){
Log.d("LOG", "Category find");
isDocPresent = true;
String catId = snapshots.get(i).getString(Statics.CATEGORY_ID_FIRESTORE);
DocumentReference documentReference = FirebaseFirestore.getInstance().collection(Statics.COLLECTION_NAME)
.document(FirebaseAuth.getInstance().getCurrentUser().getUid())
.collection(Statics.PRODUCT_COLLECTION_NAME)
.document(catId)
.collection(Statics.PRODUCT_COLLECTION_NAME)
.document();
String id = documentReference.getId();
Map<String, Object> map1 = new HashMap<>();
map1.put(Statics.PRODUCT_ID_FIRESTORE, productId);
map1.put(Statics.PRODUCT_DOCUMENT_ID_FIRESTORE, id);
documentReference.set(map1).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()){
startActivity(new Intent(AddProductSwipeCardsActivity.this, MainActivity.class));
finish();
} else {
Toast.makeText(AddProductSwipeCardsActivity.this, "Some problem occurs we add this product soon automatically", Toast.LENGTH_LONG).show();
}
});
break;
}
}
if (!isDocPresent){
Log.d("LOG", "Category not find");
DocumentReference documentReference = FirebaseFirestore.getInstance().collection(Statics.COLLECTION_NAME)
.document(FirebaseAuth.getInstance().getCurrentUser().getUid())
.collection(Statics.PRODUCT_COLLECTION_NAME)
.document();
String docId = documentReference.getId();
Map<String, Object> map1 = new HashMap<>();
map1.put(Statics.CATEGORY_FIELD_IN_FIRESTORE, categoryReceive);
map1.put(Statics.CATEGORY_ID_FIRESTORE, docId);
documentReference.set(map1).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()){
DocumentReference documentReference = FirebaseFirestore.getInstance().collection(Statics.COLLECTION_NAME)
.document(FirebaseAuth.getInstance().getCurrentUser().getUid())
.collection(Statics.PRODUCT_COLLECTION_NAME)
.document(docId)
.collection(Statics.PRODUCT_COLLECTION_NAME)
.document();
String id = documentReference.getId();
Map<String, Object> map1 = new HashMap<>();
map1.put(Statics.PRODUCT_ID_FIRESTORE, productId);
map1.put(Statics.PRODUCT_DOCUMENT_ID_FIRESTORE, id);
documentReference.set(map1).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()){
startActivity(new Intent(AddProductSwipeCardsActivity.this, MainActivity.class));
finish();
} else {
Toast.makeText(AddProductSwipeCardsActivity.this, "Some problem occurs we add this product soon automatically", Toast.LENGTH_LONG).show();
}
});
} else {
}
}
});
}
}
else {
Log.d("LOG", "null");
DocumentReference documentReference = FirebaseFirestore.getInstance().collection(Statics.COLLECTION_NAME)
.document(FirebaseAuth.getInstance().getCurrentUser().getUid())
.collection(Statics.PRODUCT_COLLECTION_NAME)
.document();
String docId = documentReference.getId();
Map<String, Object> map1 = new HashMap<>();
map1.put(Statics.CATEGORY_FIELD_IN_FIRESTORE, categoryReceive);
map1.put(Statics.CATEGORY_ID_FIRESTORE, docId);
documentReference.set(map1).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()){
DocumentReference documentReference = FirebaseFirestore.getInstance().collection(Statics.COLLECTION_NAME)
.document(FirebaseAuth.getInstance().getCurrentUser().getUid())
.collection(Statics.PRODUCT_COLLECTION_NAME)
.document(docId)
.collection(Statics.PRODUCT_COLLECTION_NAME)
.document();
String id = documentReference.getId();
Map<String, Object> map1 = new HashMap<>();
map1.put(Statics.PRODUCT_ID_FIRESTORE, productId);
map1.put(Statics.PRODUCT_DOCUMENT_ID_FIRESTORE, id);
documentReference.set(map1).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()){
startActivity(new Intent(AddProductSwipeCardsActivity.this, MainActivity.class));
finish();
} else {
Toast.makeText(AddProductSwipeCardsActivity.this, "Some problem occurs we add this product soon automatically", Toast.LENGTH_LONG).show();
}
});
}
}
});
}
}
});
【问题讨论】:
-
那么共享代码有什么问题?
-
这个方法运行了不止一次,它添加数据到触发存储不止一次
-
很可能是因为您正在实时获取数据。你试过用only a get() call吗?
-
这个函数比它运行前多了1次
-
非常感谢它有效????
标签: java android firebase google-cloud-firestore