【问题标题】:Flutter Firestore take long retrieving data while offlineFlutter Firestore 在离线时需要很长时间才能检索数据
【发布时间】:2019-08-21 11:47:24
【问题描述】:

我在颤振应用程序中使用 Firestore。每次用户启动应用程序时,它都会从 Firestore Cloud 检索一些数据。

QuerySnapshot dataSnapshot = await Firestore.instance
        .collection('/data')
        .getDocuments();

当用户第一次打开应用程序时,它要求他在线连接,获取数据,正如 Firebase 文档所说

对于 Android 和 iOS,默认情况下会启用离线持久性。要禁用持久性,请将 PersistenceEnabled 选项设置为 false。

所以,它应该保存应用程序之前读取的数据,以便在设备离线时检索它;因此用户可以随时使用已读取的相同数据访问应用程序。

问题是:设备离线时检索数据需要很长时间,并且代码相同,没有任何变化!。

我试过配置需要多少时间?离线时,大约需要 8 分 40 秒。但在线时,只需 10 秒,甚至更短。

那么我该如何解决这个问题呢?

============

更新

我设法获得了更多关于这个问题的日志,这需要很长时间,并且会使用离线保存的数据启动应用程序,它会打印这个日志

This typically indicates that your device does not have a healthy Internet connection at the moment. The client will operate in offline mode until it is able to successfully connect to the backend.

然后以 3 秒为例(时间不多)继续下一个作品。

我也确实在GitHub 开了一个新问题。

有没有办法限制时间?

【问题讨论】:

  • 我也有类似的问题。在离线情况下,最多需要 20 秒的时间来执行一个 get 操作,例如 await docRef.get()
  • 感谢分享,请为问题投票,希望能从 Flutter 和 Firebase 开发人员方面看到。

标签: firebase flutter dart google-cloud-firestore


【解决方案1】:

最后,在GitHub issue 中的diegoveloper 评论的帮助下,我找到了解决方案。

这条评论

await Firestore.instance .collection("Collection") .getDocuments(source: source)

如果我决定每次都检查源代码然后使用它,或者我可以在开始一个新的 Flutter 项目时使用它,这是一个很好的解决方案,但现在我已经有很多代码需要更好的解决方案。所以我决定分叉 cloud_firestore 包并对其进行编辑。

你可以在这里找到它:https://github.com/ShadyBoshra2012/flutterfire/tree/master/packages/cloud_firestore

我编辑的内容:

  1. firestore.dart
// The source of which the data will come from.
 static Source _source = Source.serverAndCache;

 static Source get source => _source;

 Future<void> settings(
     {bool persistenceEnabled,
     String host,
     bool sslEnabled,
     bool timestampsInSnapshotsEnabled,
     int cacheSizeBytes,
     Source source}) async {
   await channel.invokeMethod<void>('Firestore#settings', <String, dynamic>{
     'app': app.name,
     'persistenceEnabled': persistenceEnabled,
     'host': host,
     'sslEnabled': sslEnabled,
     'timestampsInSnapshotsEnabled': timestampsInSnapshotsEnabled,
     'cacheSizeBytes': cacheSizeBytes,
   });
   if (source != null) _source = source;
 }
  1. query.dart source = Firestore.source;92号线

  2. document_reference.dart source = Firestore.source;83号线

如何使用它?

因此,您可以使用 Google 的连接包以这种方式使用我的分叉存储库:https://pub.dev/packages/connectivity

pubspec.yaml 文件中添加我的分叉存储库

cloud_firestore:
    git:
      url: https://github.com/ShadyBoshra2012/flutterfire.git
      path: packages/cloud_firestore

然后在您的第一个屏幕或主屏幕中

var connectivityResult = await (Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.none) {
    await Firestore.instance.settings(source: Source.cache);
} else {
    await Firestore.instance.settings(source: Source.serverAndCache);
}

如果您想在更改连接状态时刷新源:

StreamSubscription subscription;

void initState() {
    super.initState();
    // Check the internet connection after each change
    // of the connection.
    subscription = Connectivity()
        .onConnectivityChanged
        .listen((ConnectivityResult result) async {
      // Check the internet connection and then choose the appropriate
      // source for it.
      var connectivityResult = await (Connectivity().checkConnectivity());
      if (connectivityResult == ConnectivityResult.none) {
        await Firestore.instance.settings(source: Source.cache);
      } else {
        await Firestore.instance.settings(source: Source.serverAndCache);
      }
    });
}

@override
  void dispose() {
    super.dispose();
    subscription.cancel();
  }

所以我希望它适用于每个人看到它,并等待Flutter Team编写更好的解决方案。感谢大家的参与。

【讨论】:

  • 对于新人。现在默认情况下是离线持久性。所以你不需要做这一切。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-20
  • 1970-01-01
  • 2018-03-08
  • 1970-01-01
  • 2013-10-11
  • 2012-12-04
  • 2012-12-03
相关资源
最近更新 更多