Firestore 与 Firebase 的离线问题 [英] Offline issue with Firestore vs Firebase

查看:19
本文介绍了Firestore 与 Firebase 的离线问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将我的一个应用程序转换为新的 Firestore.我正在做一些事情,例如在单击按钮时保存文档,然后在 onSuccess 侦听器中转到不同的活动.

I converted one of my apps to the new Firestore. I am doing things like saving a document on a button click, and then in the onSuccess listener, going to a different activity.

我还利用 Firestore 保存操作返回任务这一事实,使用 Tasks.whenAll 将任务组合在一起:

I also use the fact that Firestore save operations return tasks, to group tasks together using Tasks.whenAll:

val allTasks = Tasks.whenAll(
       createSupporter(supporter),,
       setStreetLookup(makeStreetKey(supporter.street_name)),
       updateCircleChartForUser(statusChange, createMode = true), 
       updateStatusCountForUser(statusChange))

      allTasks.addOnSuccessListener(this@SignUpActivity, successListener)
      allTasks.addOnFailureListener(this@SignUpActivity, onFailureListener)

最后,我从成功保存中获取文档 ID,并将其存储在首选项或本地数据库中以备后用(在 onSuccessListener 内)

Finally, I get the document id from a successful save and store it in preferences or in a local database for later use (within the onSuccessListener)

这一切都很好.直到网络连接中断.然后一切都分崩离析,因为任务永远不会完成并且 onSuccess/onFailure/onComplete 侦听器永远不会被调用.所以应用程序就挂了.

This all works great. Until there is a loss of network connectivity. Then everything falls apart, because the tasks never complete and the onSuccess/onFailure/onComplete listeners never get called. So the app just hangs.

我正在通过在每次保存之前检查网络可用性来解决这个问题,然后通过创建没有任何侦听器的任务来解决这个问题.我还在本地使用 UUID 生成器生成文档 ID.

I am working around this by checking for network availability before each save, and then doing a work-around by creating tasks without any listeners. I am also generating a document id locally using a UUID generator.

顺便说一句,这不是该应用与旧版 Firebase 一起工作的方式.在这种情况下,离线时一切运行良好,每当应用程序上线时,我都会看到文档同步.

This, BTW, was not the way the app worked with the old firebase. In that case, everything ran nicely when offline and I saw documents getting synced up whenever the app came online.

我对 Firestore 的解决方法似乎是一个可怕的黑客.有没有人想出更好的解决方案?

My workaround for Firestore seems a terrible hack. Has anyone come up with a better solution?

查看相关 Firestore 数据库没有连接时不会调用插入/删除文档回调addOnCompleteListener 未通过云 Firestore 离线调用

推荐答案

Cloud Firestore 为我们提供了处理离线数据的功能,但是您需要使用快照"(QuerySnapshot,DocumentSnapshot)来处理这种情况,不幸的是它没有很好地记录.这是一些使用 Snapshot 处理案例的代码示例(我使用 Kotlin Android):

Cloud Firestore provide us feature for handle offline data but you need to use "Snapshot" (QuerySnapshot, DocumentSnapshot) to handle this case, unfortunately it not documented well. This is some code example (I use Kotlin Android) to handle case using Snapshot:

更新数据:

db.collection("members").document(id)
  .addSnapshotListener(object : EventListener<DocumentSnapshot> {
      override fun onEvent(snapshot: DocumentSnapshot?,
                           e: FirebaseFirestoreException?) {
          if (e != null) {
              Log.w(ContentValues.TAG, "Listen error", e)
              err_msg.text = e.message
              err_msg.visibility = View.VISIBLE;
              return
          }
          snapshot?.reference?.update(data)

      }
  })

添加数据:

db.collection("members").document()
 .addSnapshotListener(object : EventListener<DocumentSnapshot> {
     override fun onEvent(snapshot: DocumentSnapshot?,
                          e: FirebaseFirestoreException?) {
         if (e != null) {
             Log.w(ContentValues.TAG, "Listen error", e)
             err_msg.text = e.message
             err_msg.visibility = View.VISIBLE;
             return
         }
         snapshot?.reference?.set(data)

     }
 })

删除数据:

db.collection("members").document(list_member[position].id)
   .addSnapshotListener(object : EventListener<DocumentSnapshot> {
       override fun onEvent(snapshot: DocumentSnapshot?,
                            e: FirebaseFirestoreException?) {
           if (e != null) {
               Log.w(ContentValues.TAG, "Listen error", e)
               return
           }
           snapshot?.reference?.delete()
       }
   })

您可以在此处查看代码示例:https://github.com/sabithuraira/KotlinFirestore 和博客发布 http://blog.farifam.com/2017/11/28/android-kotlin-management-offline-firestore-data-automatically-sync-it/

You can see code example here: https://github.com/sabithuraira/KotlinFirestore and blog post http://blog.farifam.com/2017/11/28/android-kotlin-management-offline-firestore-data-automatically-sync-it/

这篇关于Firestore 与 Firebase 的离线问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆