Markus Rautopuro
Markus Rautopuro

Reputation: 8375

How to persist Firebase objects to disk in iOS?

It seems that Firebase iOS implementation doesn't support offline caching of the client model. What this means in practice that:

The problem here is that if you're using Firebase to implement, for example a messaging app, you'd most likely want to show the user a previously cached version of the message threads and messages, before the actual connection with the backend server is established.

I'd assume the correct implementation for this would need to handle:

  1. The client-side model <-> Firebase JSON mapping (I use Mantle for this)
  2. Persisting the client-side model to disk (manual implementation using NSKeyedArchiver, or Core Data or such?)
  3. Synchronizing the on-disk model with the Firebase-linked model in memory, when the connection is available (manual implementation?)

Has anyone come up with a solution (own or 3rd party) to achieve 2) and 3)?

Upvotes: 2

Views: 2731

Answers (3)

Edward
Edward

Reputation: 2974

Just one line required for persistence with Firebase in iOS

    FIRDatabase.database().persistenceEnabled = true

Can be found here in Firebase Docs

Upvotes: 0

tfrank377
tfrank377

Reputation: 1858

It seems Firebase has solved this problem since this question was asked. There are a lot of resources on Offline Capabilities now with Firebase, including disk persistence.

For me, turning on persistence was as simple as the following in my AppDelegate:

Firebase.defaultConfig().persistenceEnabled = true

Assuming your app has been run with an internet connection at least once, this should work well in loading the latest local copy of your data.

Upvotes: 5

Tom Larkworthy
Tom Larkworthy

Reputation: 2374

There is a beta version of this technology within the client for iOS described here: https://groups.google.com/forum/#!topic/firebase-talk/0evB8s5ELmw give it a go and let the group know how it goes.

Upvotes: 3

Related Questions