首页 > 解决方案 > Firestore 写入数据有时会丢失

问题描述

我们将 Firebase Firestore 与我们简单的杂货配送应用程序 (android) 一起使用一年多。它超级快而且很棒,但它在写入时总是丢失数据。

之前,不使用transactionsand batched writes,问题更加持久(大约 10/1000 个条目)。使用后问题有所减少但并未完全消失(约 2/1000 个条目)。

场景:
我们很少有集合在添加产品时设置/更新,即products,stockdaylogs. 当我们添加/更新产品时,事务会将不同的数据写入所有这些集合。但有时(2/1000),事务成功并写入productsstock但不是daylogs

这是代码的更简单版本。虽然,我认为这不是代码问题,因为它在 99% 的时间都可以正常工作。

  firebasefirestore.runTransaction(new Transaction.Function<String>() {
            @Override
            public String apply(@NonNull Transaction transaction) throws FirebaseFirestoreException {

                DocumentReference thisProduct = rootRef.collection("products").document(docId);
                DocumentReference thisStock = rootRef.collection("stock").document(docId);
                DocumentReference logs = rootRef.collection("daylogs").document(LogDocumentId);

                transaction.update(thisProduct, data);
                transaction.update(thisStock, data);
                transaction.update(logs, data);

                String complete = "1";
                return  complete;
            }

        }).addOnSuccessListener(new OnSuccessListener<String>() {
            @Override
            public void onSuccess(String result) {
                if (result.equals("1")) {
                  Toast.makeText(AddProduct.this, "Data Added", Toast.LENGTH_SHORT).show();
                } 
            }
        });

为什么会这样?如何解决这个问题?
对不起我的英语不好。请帮忙。

标签: androidfirebasegoogle-cloud-firestore

解决方案


推荐阅读