好吧,我遇到了一个关于firestore的奇怪的问题
我有以下结构collection1 ->文档1->集合2
我正在使用on complete listener向collection2添加一个新文档(我也尝试了success listener)。未显示任何错误。新文档不会显示在控制台上。未调用监听程序。然而,当我查询时,我得到了所有添加的文档,包括新的文档。这里发生了什么事?
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();
}
}
});
发布于 2018-03-06 12:50:50
你的应用程序看起来像是离线了,或者以某种方式缺少工作的互联网连接。
当没有网络连接时,Firestore SDK在写入时不会失败。它将在本地存储写操作,并最终将其同步到服务器。同时,本地写入成为任何查询的一部分,就像数据在服务器上是否可用一样。因此,您所看到的可能是本地编写的结果。
(这与您在实时数据库中看到的行为相同。)
至于为什么Firestore SDK没有连接,我不知道。你可能需要自己解决这个问题。
https://stackoverflow.com/questions/49123435
复制相似问题