gpt4 book ai didi

react-native - 从 Firebase JS SDK (Web) 迁移到 react-native-firebase 以进行离线存储

转载 作者:行者123 更新时间:2023-12-04 17:35:14 27 4
gpt4 key购买 nike

我一直在将 Firebase Web SDK 用于我的 react-native 应用程序(我正在使用 FIRESTORE 来存储数据)。到目前为止,我没有遇到任何问题。一切顺利。但现在我想在我的应用程序中添加某种离线存储机制,这样即使我的用户处于离线状态,我仍然可以提供一些功能或显示从上次连接的 session 中缓存的一些内容。经过一些调查,我的印象是 react-native-firebase是首选方式。现在我有一些疑问,我想从有经验的人那里得到一些建议。

  • react-native-firebase 是唯一的选择吗?我很快就读到了 AsyncStorage它只是一个键值存储。考虑到我想做的最简单的事情是翻阅 firestore 文档列表,这种存储似乎不适合离线执行此操作。就像如果我想用 AsyncStorage 做到这一点,我将不得不把我从 firestore 后端获得的所有内容(可能是数百个文档)放在一起,将它们作为单个字符串值保存,取回它们,解析它们,对它们进行分页等。并且为所有这些编写自定义逻辑和方法。
  • 如果我要使用 react-native-firebase,只需启用离线存储 - 我假设 - 会为您解决这个问题,您不必为离线存储使用编写任何自定义逻辑。我假设为离线使用而保留的数据与 firestore 数据库中的结构相同。我觉得如果我使用 react-native-firebase 以外的任何东西,我将不得不自己处理所有用于持久化、读取和离线呈现数据的自定义逻辑。对吗?
  • 我最担心的是可能需要进行大量代码重构。我有很多行代码和很多 .get().then() 之类的行,我从 firestore 获取和呈现数据。在 react-native-firebase 的文档中它说:

...aims to mirror the official Firebase Web SDK as closely as possible.

我不确定这在多大程度上是正确的。我检查了 react-native-firebase 的 firestore 模块的 reference documentation但我无法确定实际支持了多少这些查询方法。

那么,要走的路是 react-native-firebase 的路吗?尝试重构现有代码是否会对我造成重大损失?你有类似的经历吗?我将不胜感激任何帮助。

非常感谢...

最佳答案

这里是 react-native-firebase 库的维护者。

...aims to mirror the official Firebase Web SDK as closely as possible.

这是一个次要的免责声明,因为两者之间存在一些差异,主要是某些事情必须如何使用 React Native 来实现。

例如,enablePersistence 在 RNFB 上不存在。相反,持久性在默认情况下处于启用状态,并且可以通过 settings() 关闭(或打开)。

Is react-native-firebase the only option to go? I have quickly read about AsyncStorage and it is just a key-value storage. Considering the simplest thing I want to do is page through a list of firestore documents, this kind of storage seems not to be suitable to do this offline. Like If I wanted to do this with AsyncStorage I would have to put all the content (maybe hundreds of documents) I get from the firestore backend, persist them as a single string value, fetch them back, parse them, page them etc. And write custom logic& methods for all these.

这在技术上是可行的,但是正如您所提到的,这也有缺点。使用 Firestore,当设备离线(在应用程序中很常见)并且您尝试读/写时,它会读取/更新您的本地缓存,这仍然会触发事件监听器。当该应用重新联机时,它会自动为您与服务器重新同步。

If I was to use react-native-firebase, just enabling the offline storage -I assume- takes care of this for you and you don't have to write any custom logic for offline storage usage. I assume the data that has persisted for offline usage has the same structure as it does in firestore database. I feel like If I use anything other than react-native-firebase, I would have to handle all the custom logic for persisting, reading and rendering the data offline myself. Is that right?

这一切都为您处理。我们围绕原生 Firebase SDK,因此如果您在不使用 React Native 的情况下开发原生 Android/iOS 应用程序,则期望获得相同级别的一致性。

The biggest concern I have is the amount of code refactoring that might be required. I have many lines of code and so many .get().then() like lines where I get and render the data from firestore.

由于上述原因,除了一些小方法外,通常一切都是一样的。

So, the way to go is react-native-firebase's way? Would it take a heavy toll on me trying to refactor the existing code? Any similar experience do you have? I would appreciate any help.

我建议任何使用 React Native 和 Firebase 进行开发的人都使用 RNFB。它提供了许多 Web SDK 无法通过 React Native 提供的额外功能。除了更繁琐的设置和更改导入之外,它的工作原理应该非常相似。

关于react-native - 从 Firebase JS SDK (Web) 迁移到 react-native-firebase 以进行离线存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56951166/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com