java - 在 Firestore : detect if the operation is successful on the server, 本地存储文档还是不成功?

标签 java android google-cloud-firestore

我正在使用 Google Firestore 编写 Java Android 应用程序。当用户创建数据时,它们必须存储在 Firestore 上(用户单击“提交”按钮)。应用程序显示进度条,直到存储成功或失败。如何检测数据是否存储(本地或远程服务器上)?我想告诉用户存储是否成功(数据存储在服务器上)、本地成功(服务器无法访问,但离线存储可以)或不成功。

为了在服务器上完成存储,可以通过在任务上注册 OnCompleteListener 来轻松检测到这一点。

但是当服务器不可达时,监听器不会被调用(事实上,稍后当服务器再次可达时才会调用,但用户不能等这么久!)。我发现可以通过在 DocumentReference 上注册的 EventListener 来通知数据存储在本地。此监听器可以使用 documentSnapshot.getMetadata().isFromCache() 和 documentSnapshot.getMetadata().hasPendingWrites() 获取信息。但是当服务器在线并且数据存储在服务器上时,该监听器也会收到通知。

 progressBar.setVisibility(View.VISIBLE);
 final DocumentReference documentReference =
 FirebaseFirestore.getInstance().document(documentFirestorePath);
 documentReference. addSnapshotListener(this, MetadataChanges.INCLUDE,
 new EventListener<DocumentSnapshot>() {
     @Override
     public void onEvent(@Nullable final DocumentSnapshot documentSnapshot, @Nullable final FirebaseFirestoreException e) {
         Log.i("MyActivity", "isFromCache: "+documentSnapshot.getMetadata().isFromCache()+", write pending:
 "+documentSnapshot.getMetadata().hasPendingWrites());
     } });
 documentReference.set(newContact).addOnCompleteListener(new OnCompleteListener<Void>() {
     @Override
     public void onComplete(@NonNull final Task<Void> task) {
         Log.i("MyActivity", "completed");
         progressBar.setVisibility(View.INVISIBLE);
         if (task.isSuccessful()) {
             finish();
         } else {
             // Inform the customer the action failed
         }
     }
  });

以下是服务器在线时的日志:

2019-06-13 08:11:14.126 I/MyActivity: isFromCache: true, write pending: false
2019-06-13 08:11:14.170 I/MyActivity: isFromCache: true, write pending: true
2019-06-13 08:11:14.218 I/MyActivity: isFromCache: false, write pending: true
2019-06-13 08:11:14.470 I/MyActivity: completed
2019-06-13 08:11:14.552 I/MyActivity: isFromCache: false, write pending: false

以及服务器离线时的日志:

2019-06-13 08:12:08.185 I/MyActivity: isFromCache: true, write pending: false
2019-06-13 08:12:08.215 I/MyActivity: isFromCache: true, write pending: true
And when the server is back, the following logs are added:
2019-06-13 08:12:40.460 I/MyActivity: isFromCache: false, write pending: true
2019-06-13 08:12:40.671 I/MyActivity: completed
2019-06-13 08:12:40.753 I/MyActivity: isFromCache: false, write pending: false

最佳答案

最后,告诉用户他的修改已存储(本地或服务器上)就足够了。

所以

addSnapshotListener(this, MetadataChanges.INCLUDE,
 new EventListener<DocumentSnapshot>()

就足够了:检查的条件是 hasPendingWrites == true。

不需要 OnCompleteListener。

关于java - 在 Firestore : detect if the operation is successful on the server, 本地存储文档还是不成功?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56574401/

相关文章:

android - 如何从 Firestore 中检索数组?

java - 异常 : org. hibernate.SessionException: session 已关闭

android - 如何开启PDF417对条码扫描的支持?

java - Guava checkNotNull 有什么意义

android - 像在 android L 中一样实现 NavigationDrawer 图标

java - 在Android上使用jxl修改excel电子表格

java - 我可以在 firestore 查询中设置多个 .whereEqualTo 指向文档中的字段吗?

flutter - 将 Firestore DocumentSnapshot 转换为 Flutter 中的 map

java - 正确的 Vertx 数据库礼仪

java - 有没有办法通过注释在metro中实现ws-security?