本文介绍了如何将Firebase数据缓存为离线使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我构建捆绑JSON数据的应用程序。我想要切换到Firebase作为我的后端,但是我需要确保即使Firebase处于脱机状态也可以访问数据。不能保证用户在启动应用程序的时候能够连接到互联网。数据由一个相当大的JSON blob组成。



我听说firebase会将数据缓存在iOS上进行离线访问,这很好。我只需要知道如何在第一次运行应用程序时绑定数据,所以用户可以在进入网络连接之前使用应用程序。

解决方案

正如您发现的那样,Firebase Obj-C客户端对脱机访问/磁盘持久性具有beta支持。有关详情,请参阅。



但是这并没有解决您希望使用初始数据对应用程序进行种子处理,以便在应用程序能够连接到Firebase之前获得数据。不幸的是,没有直接的支持。

一个你可以尝试使用Firebase的解决方法是只用一个setValue来处理有问题的数据, 。这应该可以工作,但最终会尝试将这些数据写入Firebase,当应用程序连接时,您可能需要安全规则来防止用户实际修改该数据。正如我所说,这将是一个hacky的解决方案。

现在最好只是用你的应用程序中的特殊逻辑处理这个从其他数据来源(硬编码值或嵌入式文件或其他),直到您第一次从Firebase获取数据。



对不起,目前没有直接的支持。我们会看看是否可以在未来更直接地支持。


I build apps that bundle up JSON data. I want to switch to Firebase as my backend, but I need to ensure I can access the data even if firebase is offline. There's no guarantee that the user will have an internet connection at the time they launch the app. The data consists of a fairly large JSON blob.

I heard that firebase does cache data on iOS for offline access, and that's great. I just need to know how to bundle the data for that first time the app is ran, so the user can use the app prior to getting to a network connection.

解决方案

As it sounds like you discovered, the Firebase Obj-C client does have beta support for offline access / disk persistence. Details can be found here.

But that doesn't address your desire to "seed" the app with initial data so that it has data available before the app has ever been able to connect to Firebase. Unfortunately, there's no direct support for that.

One hacky solution you could attempt with Firebase is to just do a setValue with the data in question, in order to seed the cache. This should work but will eventually try to write that data to Firebase, when the app gets connected, so you'd probably want to have security rules to prevent the user from actually modifying that data. As I said, it'd be a hacky solution.

For now it might be best to just handle this with special logic in your app that pulls data from some other data source (hardcoded values or an embedded file or whatever) until the first time you get data from Firebase.

Sorry there's no direct support for this at the moment. We'll take a look to see if we could support this more directly in the future.

这篇关于如何将Firebase数据缓存为离线使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 12:34