在 Flutter 应用程序中使用 Firestore 中的离线持久性 [英] Using Offline Persistence in Firestore in a Flutter App

查看:17
本文介绍了在 Flutter 应用程序中使用 Firestore 中的离线持久性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用 Firebase 的 Firestore 将数据发送到网络的应用.该应用程序的功能之一是能够在离线时将数据保存在设备中,并在互联网连接恢复时将其发送到 Firestore.我激活了离线持久化,但它不起作用.

I'm developing a app that uses Firebase's Firestore to send data to the web. One of the functions of the app is being able to save data in the device while being offline and send it to Firestore when internet connection is restored. I activated offline persistence but it dosen't work.

调试控制台:

W/OkHttpClientTransport(28536): Failed closing connection
W/OkHttpClientTransport(28536): javax.net.ssl.SSLException: Write error: ssl=0x7f7acfc408: I/O error during system call, Broken pipe
W/OkHttpClientTransport(28536):     at com.google.android.gms.org.conscrypt.NativeCrypto.SSL_write(Native Method)
W/OkHttpClientTransport(28536):     at com.google.android.gms.org.conscrypt.NativeSsl.write(:com.google.android.gms@14798020@14.7.98 (040406-222931072):4)

如何在互联网恢复后激活离线持久性并与 Firestore 同步?

How can I activate offline persistence and sync with Firestore when internet is restored?

我的代码:

Future<Null> sendFirebaseData(var selectedModel) async {

    Firestore.instance.enablePersistence(true);
    var certID = await getIDCertificado();

    var dateTime = new DateTime.now();
    var nowHour = new DateFormat('kk:mm:ss').format(dateTime);

      Map<String, dynamic> dataHeader = {
        'ID':                certID,
        };

      Map<String, dynamic> finalDataMap = {}..addAll(dataGeneral)
                                          ..addAll(dataInstrumento)..addAll(dataPadrao)
                                          ..addAll(dataAdicional)..addAll(dataHeader);

      await Firestore.instance.collection('certificados').document((certID.toString()))
          .setData(finalDataMap);}

推荐答案

当你在 Firestore 中使用离线持久化时,不要使用 Transactions 或 await 来响应.

when you use offline persistence in Firestore, don't use Transactions or await for response.

所以,改变这个:

  await Firestore.instance.collection('certificados').document((certID.toString()))
      .setData(finalDataMap);

为此:

 Firestore.instance.collection('certificados').document((certID.toString()))
      .setData(finalDataMap);

当您恢复互联网连接时,即使您在后台,您的数据也会自动同步.

When you restore your internet connection your data will be sync automatically, even if you are in background.

当您的应用关闭时不起作用.

Doesn't work when your app is closed.

为什么上面的代码更改为删除await"?有效.

Why the above code change to remove "await" works.

参考:Firebase 视频 - 如何启用离线支持 11:13

在服务器上的文档写入成功之前,不会调用您的回调并且您的承诺不会完成.这就是为什么如果您的 UI 等到写入完成才执行某些操作,它似乎会冻结在离线模式"中的原因.即使写入实际上是对本地缓存进行的.

Your callback won't be called and your promise won't complete until the document write has been successful on the server. This is why if your UI waits until the write completes to do something, it appears to freeze in "offline mode" even if the write was actually made to the local cache.

不使用async/await.then()callbacks 是可以的.Firestore 将始终行动"就像立即应用数据更改一样,因此您无需等待处理新数据.

It is OK to not use async / await, .then() or callbacks. Firestore will always "act" as if the data change was applied immediately, so you don't need to wait to be working with fresh data.

您只需要在需要确保服务器写入已经发生时才需要使用回调和承诺,并且您想阻止其他事情发生,直到您得到确认.

You only need to use callbacks and promises when you need to be sure that a server write has happened, and you want to block other things from happening until you get that confirmation.

这篇关于在 Flutter 应用程序中使用 Firestore 中的离线持久性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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