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

问题描述 投票:1回答:1

好吧,我遇到了firestore的一个奇怪的问题

我有以下结构集合1 - > document1-> collection2

我正在使用完整的监听器向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();
                }
            }
        });
android firebase google-cloud-firestore
1个回答
2
投票

您的应用程序表现得像离线或某种程度上缺乏可用的互联网连接。

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

(这与您在实时数据库中看到的行为相同。)

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

© www.soinside.com 2019 - 2024. All rights reserved.