【问题标题】:Function runs repeat automatically android studio函数运行自动重复android studio
【发布时间】: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


【解决方案1】:

此方法运行多次,并将数据添加到 Firestore 多次。

这是因为您使用Query#addSnapshotListener(EventListener listener) 实时获取数据。这意味着在引用指向的集合中发生的每个添加/更新/删除操作都会调用侦听器,因此会产生这种行为。

为避免这种情况,您应该简单地使用Query#get() 调用,只获取一次数据。

执行查询并将结果作为 QuerySnapshot 返回。

有关如何使用 get() 从 Firestore 获取数据的更多信息,请查看以下文章,其中我解释了 4 种不同的方法:

【讨论】:

    猜你喜欢
    • 2015-08-07
    • 2013-06-12
    • 2014-03-23
    • 2019-10-27
    • 2019-12-12
    • 1970-01-01
    • 2021-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多