How to save a part of firebase realtime database in offline mode

前端 未结 2 1588
梦谈多话
梦谈多话 2021-01-27 09:01

I wanted to store the specific path in Firebase database (JSON) in local phone storage, not all data at the realtime-Firebase database. Say I have a news-feed path for each user

相关标签:
2条回答
  • 2021-01-27 09:46

    According to me the best way will be to parse the Firebase JSON data and map it into an object and then save only data that you need in a local storage (News-feed in your case), and then access it later whenever you need it.

    For this, you can use Paper DB as a local storage to store specific data and use it whenever you need to. It stores data as a key value pair so you can access your data with the same key you inserted it with in the database. (Just like shared preferences work). It stores data as cache in your local storage and uses Kryo serialization framework which is pretty fast for I/O operations.

    Or you can also use Room (a google library) with SQLite to achieve this task. Haven't tried Room but i think it will suite your purpose. Here's the official documentation for Room

    0 讨论(0)
  • 2021-01-27 10:04

    Those two bits of code are not really related. They don't do the same thing.

    keepSynced(true) effectively keeps a listener active on the given reference, from the moment it's called, for as long as the app is running, so that the local version of the data is always in sync with the remote version on the server.

    setPersistenceEnabled(true) just activates local caching of the data read by the SDK. When persistence is enabled, the app can still query data previously read. It takes effect for all data read by the SDK. While persistence is enabled, you can't control which data is cached - all read data is cached up to 10MB max. When the max is reached the oldest data will be evicted from the cache.

    0 讨论(0)
提交回复
热议问题