【问题标题】:Is there a way for Firebase Flutter apps to persist data across offline app restarts?Firebase Flutter 应用程序有没有办法在离线应用程序重启时保留数据?
【发布时间】:2018-07-11 15:11:40
【问题描述】:

我正在使用 firebase 动画列表来显示存储在 firebase 实时数据库中的项目列表。

现在当我在 offline 状态下启动应用程序(应用程序被杀死之后),列表不会加载。

是否有内置方法可以离线存储此用例的数据(由 firebase 提供)?还是我需要存储在一些离线数据库中?

我检查了https://firebase.google.com/docs/database/android/offline-capabilities,但找不到这个特定的用例。

相关问题https://github.com/flutter/flutter/issues/19271

【问题讨论】:

  • Firebase 实时数据库还是 Firebase Cloud Firestore?
  • 用于 FIrebase 实时数据库
  • 有什么解决办法吗?

标签: firebase flutter


【解决方案1】:

听起来像FirebaseDatabase.setPersistenceEnabled

  /// Attempts to sets the database persistence to [enabled].
  ///
  /// This property must be set before calling methods on database references
  /// and only needs to be called once per application. The returned [Future]
  /// will complete with `true` if the operation was successful or `false` if
  /// the persistence could not be set (because database references have
  /// already been created).
  ///
  /// The Firebase Database client will cache synchronized data and keep track
  /// of all writes you’ve initiated while your application is running. It
  /// seamlessly handles intermittent network connections and re-sends write
  /// operations when the network connection is restored.
  ///
  /// However by default your write operations and cached data are only stored
  /// in-memory and will be lost when your app restarts. By setting [enabled]
  /// to `true`, the data will be persisted to on-device (disk) storage and will
  /// thus be available again when the app is restarted (even when there is no
  /// network connectivity at that time).
  Future<bool> setPersistenceEnabled(bool enabled) async {
    // ...
  }

来自firebase_database/lib/src/firebase_database.dart

【讨论】:

  • 如果“重新启动”被解释为应用程序进入后台并且没有被杀死,那么是的,setPersistenceEnabled 有效,但我的用例是应用程序被杀死/终止然后启动(处于离线状态)。
  • "persisted to on-device (disk)" 应该在终止的应用程序中继续存在。如果它不起作用,请报告错误。确保您已尝试使用最新的插件版本,如果可能还尝试使用 Flutter dev 频道。
  • FirebaseDatabase.instance.setPersistenceEnabled(true);您可能在 .setPersistenceEnabled 之前忘记了 .instance*。
  • 这已在不久前更改。从那以后我就没用过。
猜你喜欢
  • 1970-01-01
  • 2021-07-05
  • 1970-01-01
  • 2020-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-11
  • 1970-01-01
相关资源
最近更新 更多