Firestore与Firebase的脱机问题 [英] Offline issue with Firestore vs Firebase

查看:138
本文介绍了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?

请参阅相关的没有连接时不会调用插入/删除文档回调 addOnCompleteListener不能通过云Firestore脱机

推荐答案

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

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天全站免登陆