首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >控制台查询添加文档但未添加,也未调用监听器

控制台查询添加文档但未添加,也未调用监听器
EN

Stack Overflow用户
提问于 2018-03-06 12:34:01
回答 1查看 38关注 0票数 1

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

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

我正在使用on complete listener向collection2添加一个新文档(我也尝试了success listener)。未显示任何错误。新文档不会显示在控制台上。未调用监听程序。然而,当我查询时,我得到了所有添加的文档,包括新的文档。这里发生了什么事?

代码语言:javascript
运行
复制
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();
                }
            }
        });

下面是我执行的查询

代码语言:javascript
运行
复制
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();
                }
            }
        });
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-06 12:50:50

你的应用程序看起来像是离线了,或者以某种方式缺少工作的互联网连接。

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

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

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49123435

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档