Ninja420
Ninja420

Reputation: 3872

Firebase Cache vs Implementing a Custom Room Cache

We are using firebase realtime database and I was thinking about implementing a cache locally to reduce repeated calls.

So I came up with an algorithm which involves room persistence library.

stream only the latest data from firebase
   store in room cache
when requested fetch all data from room cache and return

But then I started thinking about the cache that firebase provides and started realizing that I might be able to avoid room library at all.

stream only the latest data from firebase
when requested call fetch data using a singleValueListener

Since we are using single value listener only the cached data would be fetched from firebase.

What are the drawbacks of using the second approach? I know that firebase cache is limited to 10MB so that might be one

Upvotes: 1

Views: 563

Answers (2)

Doug Stevenson
Doug Stevenson

Reputation: 317497

The one big thing you need to know here is that the cache managed by the SDK is almost fully outside your control. You can set the size of the cache, and you could clear it by trying to find the database file it uses, but otherwise, you can't configure it.

If you write code on your own, will have to make every decision about how it works, and it will be a lot of code to get everything right.

Upvotes: 1

Alex Mamo
Alex Mamo

Reputation: 138834

Unlike in Cloud Firestore, where offline persistence is enabled by default, in Firebase realtime database to enable offline capabilities you need to use the following line of code:

FirebaseDatabase.getInstance().setPersistenceEnabled(true);

This means that you'll have by default a local copy of your database. So there is no need to add another one. So in this way you'll only get new data, otherwise everyhing is you get is from cache.

Upvotes: 0

Related Questions