Android-Firebase离线最佳做法 [英] Android - Firebase Offline Best Practices

查看:250
本文介绍了Android-Firebase离线最佳做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我坚持理解使Firebase App脱机的概念. 根据文档,我们需要调用:

FirebaseDatabase.getInstance().setPersistenceEnabled(true);

但是我们应该在哪里称呼它呢?是否应在应用程序的每个活动中使用它?因为,当我在Application类的onCreate()方法中使用它时,我的应用程序连续崩溃.

那么我们应该遵循的最佳实践是使我们的应用脱机.

另一个疑问是上述内容与DatabaseReference.keppSynced(true);

之间的区别 在检索数据库的第一个引用之前,应在启动时调用

解决方案

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

这篇关于Android-Firebase离线最佳做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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