且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android-Firebase离线***做法

更新时间:2023-12-05 21:58:04

setPersistenceEnabled().我打电话给FIRApp.configure()

后就直接打电话给我

启用持久性允许完全脱机保留信息.这样做的重要组成部分是,即使您强制关闭并重新打开应用程序,脱机请求和更新也将完成.如果您要查找脱机访问权限,则需要设置布尔值.

如果需要在每次检索信息时都希望缓存最新数据,但不一定需要在下次打开应用程序时还原该数据,则使用保持同步".

一个很好的使用示例是,如果您有访问登录用户详细信息的视图.您可以仅指定保持该数据库引用同步,而不必更快地获取最新数据,而不必在使用这些细节的所有视图上都使用用户详细信息侦听器.

有关持久性和同步的更多详细信息,请参阅Firebase文档: https://firebase.google.com/docs/database/android/offline-功能

I am stuck at understanding the concept of making Firebase App Offline. As per the documentation, we need to call:

FirebaseDatabase.getInstance().setPersistenceEnabled(true);

But where should we call this? Is it should be used in every Activity of the Application? Because, when I am using this inside my Application class onCreate() method, my app crashes continuously.

So what is the best practice we should follow to make our app offline.

One more doubt is the difference between the above one and DatabaseReference.keppSynced(true);

setPersistenceEnabled() should be called once on startup, before you retrieve your first reference of the database. I call mine directly after I call FIRApp.configure()

Persistence enabled allows for complete offline retention of information. The significant component of this is that offline requests and updates will be completed, even if you force-close the app and reopen it. If you're looking for offline access that's the boolean to set.

Keep sync is used for if circumstances where you want up to date data cached for whenever you retrieve the information, but don't necessarily need that data to be restored when you next open the app.

A good example of use would be if you had views which accessed the logged in user details. Instead of having user detail listeners on all views which use the details, you can just specify to keep that database reference synced and you can get up to date data quicker.

More details on persistence and syncing can be read here in the Firebase documentation: https://firebase.google.com/docs/database/android/offline-capabilities