Have you ever had an app suddenly stop working when you lost your internet connection? Or perhaps you’ve opened an app you were using just minutes prior, only to find you have to re-download all of your data?
As a developer, you’ve likely tried to solve these pain points in your own apps, only to find the work required to be daunting. Wouldn’t it be great if your database could just handle this for you automatically?
Firebase Offline to the Rescue
Today we’re announcing a set of features for our iOS and Android SDKs that will allow you to build apps that work smoothly offline!
Our new SDKs support persisting your synchronized data to disk, so it’s available immediately when your app starts. You can enable disk persistence with one line of code:
Firebase.getDefaultConfig().setPersistenceEnabled(true);
[Firebase defaultConfig].persistenceEnabled = YES;
Firebase.defaultConfig().persistenceEnabled = true
Our new SDKs now also let you specify data to be prefetched and kept up to date so that it will be available offline later if you need it. This keepSynced
feature can be enabled on a per-path basis with one line of code:
ref.keepSynced(true);
[ref keepSynced:YES];
ref.keepSynced(true)
Built from the Beginning for Offline
The Firebase database uses synchronization rather than request / response to move data between client and server. All read and write operations happen against a local, on-device version of your database first. Then, data is pushed and pulled from our servers behind the scenes. This design has allowed the Firebase database to compensate for network latency — local write operations are reflected by local reads before being acknowledged by the server.
It turns out, though, that this design is also essential to a high quality offline experience. By sending all reads and writes through a local version of the database, the Firebase SDK maintains the freedom to serve that data from the best source available — whether that’s from the network or local disk. Later, when a network connection becomes available, our SDK automatically commits local operations to the server and pulls in the latest remote updates.
We designed Firebase for offline support from the very beginning, and we’ve been working on it for years. Citrix has been beta testing our offline support for its iOS app Talkboard since 2013. When you draw on a canvas in Talkboard, any updates you make while offline will be saved to disk and synchronized later when you reopen the app.
Getting Started
We’ve been working on these features for a long time, and we couldn’t be more excited to see what you build with them! Getting started is easy — just check out the offline section of our guides for iOS or Android. We also have offline drawing examples for iOS and Android that can help you get started quickly.
As you’re coding, we’d love to hear what you think! Please share your feedback in our Google Group or mention @Firebase on Twitter.