已查询添加文档,但未在控制台上添加,也不调用侦听器



好的,我在火库遇到了一个奇怪的问题

我有以下结构集合1 ->文档1->集合2

我正在向collection2添加一个新文档,并在完整的侦听器上(我也尝试了成功的侦听器(。 不显示任何错误。新文档不会显示在控制台上。未调用侦听器。但是,当我查询时,我得到了所有添加的文档,包括新文档。这是怎么回事?

Map<String, Object> data = new HashMap<>();
data.put("completed", true;
data.put("date_completed", new Date()); 
data.put("location", "123 main st");
data.put("notes", "");
data.put("work", "");
db.collection("collection1").document(document1).collection("collection2")
        .add(data)
        .addOnCompleteListener(new OnCompleteListener<DocumentReference>() {
            @Override
            public void onComplete(@NonNull Task<DocumentReference> task) {
                if (task.isSuccessful()) {
                    DocumentReference documentReference = task.getResult();
                    Log.d(TAG, "DocumentSnapshot written with ID: " + documentReference.getId());
                    det.setDocId(documentReference.getId());
                    addr_arr.add(det);
                } else {
                    Log.w(TAG, "Error adding document", task.getException());
                    Toast.makeText(EditListActivity.this, "Failed operation - " + task.getException().getMessage(),
                            Toast.LENGTH_SHORT).show();
                    hideProgressDialog();
                }
            }
        });

这是我做的查询

CollectionReference collecRef = db.collection("collection1").document(document1).collection("collection2");
        collecRef.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    for (DocumentSnapshot document : task.getResult()) {
                         // here I do document.get on all the fields, add them to object and add object to array
                    }
                    adapter.notifyDataSetChanged();
                } else {
                    Log.d(TAG, "get failed with ", task.getException());
                    Toast.makeText(EditListActivity.this, "Failed operation - " + task.getException(),
                            Toast.LENGTH_SHORT).show();
                    hideProgressDialog();
                }
            }
        });

您的应用表现得像是离线状态,或者缺少有效的互联网连接。

当没有网络连接时,Firestore SDK 不会在写入时失败。 它将在本地存储写入,并最终将其同步到服务器。 同时,本地写入将成为任何查询的一部分,就像数据在服务器上可用一样。 因此,您看到的可能是本地写入的结果。

(这与实时数据库的行为相同。

至于为什么Firestore SDK没有连接,我不知道。 您可能需要自行解决此问题。