简体   繁体   English

如何在 Flutter 应用程序中以离线模式使用 firebase 实时数据库?

[英]How to use firebase realtime-database in offline mode in Flutter app?

I came across a wonderful feature of Firebase offline feature.我遇到了 Firebase 离线功能的一个很棒的功能。 I integrated that in my app just by writing one line of code in my main.dart file after initializing Firebase await FirebaseDatabase.instance.setPersistenceEnabled(true);在初始化 Firebase await FirebaseDatabase.instance.setPersistenceEnabled(true);

Question 1:问题一:
I couldn't able to understand the database.keepSynced(true) function because without using this line of code, my app is persisting old as well as fetching new updated data, so what this exactly does?我无法理解database.keepSynced(true) function,因为如果不使用这行代码,我的应用程序会保留旧数据并获取新的更新数据,那么这到底是做什么的呢?

Question 2:问题2:
How could I prevent the write operations when a user is offline, because I read that after setting persistence enabled, it makes a queues of write operations and update them when user gets online, so how could I stop this?当用户离线时如何阻止写操作,因为我读到在启用持久性后,它会生成一个写操作队列并在用户在线时更新它们,所以我怎么能阻止它呢?

Question 3:问题三:
Is this persistence feature going to work in IOS device as well or need some permission settings first?此持久性功能是否也适用于 IOS 设备,还是需要先进行一些权限设置?

Thanks谢谢

When you call FirebaseDatabase.instance.setPersistenceEnabled(true) you're allowing Firebase to create a local file on the device where it persists any data it's recently read, and all writes that are pending while the device is offline.当您调用FirebaseDatabase.instance.setPersistenceEnabled(true)时,您允许 Firebase 在设备上创建一个本地文件,它保存最近读取的任何数据,以及设备离线时所有待处理的写入。

When you call keepSynced(true) on a node, you are telling the SDK to always keep that node synchronized.当您在节点上调用keepSynced(true)时,您是在告诉 SDK 始终保持该节点同步。 It essentially creates a onValue listener on the node without any handler code, so you're purely doing this to keep the data synchronized for when the device does go offline.它实际上在没有任何处理程序代码的节点上创建了一个onValue侦听器,因此您这样做纯粹是为了在设备go脱机时保持数据同步。

By combining keepSynced(true) with setPersistenceEnabled(true) , you're specifying that you want the app to continue working when it's offline across restarts, and which data is needed for that.通过结合keepSynced(true)setPersistenceEnabled(true) ,您可以指定您希望应用程序在重新启动时脱机时继续工作,以及为此需要哪些数据。

If you call keepSynced(true) on the root of your database, you're telling the SDK to synchronize all data in the database to the disk cache.如果您在数据库的根目录上调用keepSynced(true) ,您就是在告诉 SDK 将数据库中的所有数据同步到磁盘缓存。 While this may initially be a quick way to get offline mode for your app working, it typically won't scale when you more people start using your app.虽然这最初可能是让您的应用程序运行离线模式的快速方法,但当您有更多人开始使用您的应用程序时,它通常不会扩展。


If you only want to allow write operations while the client has a connection to the database backend, you can register a local listener to the .info/connected node, which is a true value when there is a connection and false otherwise.如果您只想在客户端连接到数据库后端时允许写操作,您可以向.info/connected节点注册一个本地侦听器,当有连接时为true值,否则为false

Note that Firebase doesn't require this, as it queues the pending writes and executes them when the connection is restored.请注意,Firebase 不需要这样做,因为它会将挂起的写入排队并在连接恢复时执行它们。 In general, I'd recommend working with the system here instead of against it, and also trying to make your app work gracefully in the offline scenario.总的来说,我建议在这里系统一起工作而不是反对它,并尝试让您的应用程序在离线场景中优雅地工作。 In many cases there is no need to disable functionality while the app is offline.在许多情况下,应用程序处于离线状态时无需禁用功能。


Offline disk persistence is available on Android and iOS, but not on web.离线磁盘持久化在Android和iOS可用,但在web不可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM