firebase Firebase Offline Capabilities

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Introduction

In this post you will find the different ways to implement offline capabilities when usingFirebase , information about when and why could be a good idea enable offline capabilities and examples of it with Android platform.

Remarks

What should I use? Disk persistence or keepSynced calls?


From my experience I can say that it always depends of what your app is working, and how you manage the transactions and database of your application. If for example you have an application where the user is just writing and reading data but he is not able to delete or edit it, use DiskPersistence would be the right choice.

Also, DiskPersistence will store data in cache, which means that your app will use more space in the user's devices, which maybe is not the best idea in your case.

In other hand, if your application manages a lot of complex transactions and your data is updated really often, possibly you should avoid DiskPersistence and use keepSynced in the references that you want to keep fresh.

Why?


DiskPersistence stores the data retrieved in local, which sometimes can cause lot of desynchronization showing your data if you don't use it together with continous ListenerValueEvents. For example:

  1. User A writes a message "Hello World" on your app, which is recieved for user B
  2. User B download message from User A in his phone and see the message "Hello World"
  3. User A edit's his message to "Firebase is cool".
  4. User B will still watching "Hello World" message even if he updates the data cause the snapshot ref is the same when Firebase filter for it.

To avoid this the best idea is keep continous listeners in the references that you want to track all time.

Can I use both together?


Of course you can, and in most of the apps possibly is the best idea to avoid download a lot of data and give the user the possibility to work with your app even if he has no connection.

If you don't care about use cache space in the user device, I recommend you to enable diskPersistence in your FirebaseDatabaseobject and also add a keepSync flags to each reference that can have a lot of times in a short space time or you want to keep fresh all time.



Got any firebase Question?