Firebase Offline Capabilities
In this post you will find the different ways to implement offline capabilities when using
Firebase , information about when and why could be a good idea enable offline capabilities and examples of it with Android platform.
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.
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.
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:
- User A writes a message "Hello World" on your app, which is recieved for user B
- User B download message from User A in his phone and see the message "Hello World"
- User A edit's his message to "Firebase is cool".
- 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.
Enable disk persistence (Android / iOS only)
To enable disk persistence you should enable the flag
persistenceEnabled in the
FirebaseDatabaseInstance object of your application:
If you want to disable the persistence in some moment of your app lifecycle you should remember to disable it in the same way:
Keeping data fresh (Android/iOs Only)
Firebase synchronizes and stores a local copy of the data for active listeners when used on mobile devices. In addition, you can keep specific locations in sync.
Firebase client automatically downloads the data at these locations and keeps it updated even if the reference has no active listeners. You disable synchronization with the following line of code.