I've been reading a lot on S.O. about offline (local) storage and AngularFire, and I understand that AngularFire is intended for 3 way binding usage with the Angular model and DOM.
With specific reference to How to sync offline database with Firebase when device is online? I can see that it's not a trivial matter introducing local storage into the mix. I didn't quite understand the concept of "priming" Firebase in that thread.
However, I do need to be able to start the app in offline mode. Here's my scenario:
I know Kato also has mentioned that there may be offline storage capabilities coming in a future release of AngularFire - is that still a possibility? Otherwise my thinking is to do this:
It doesn't feel like a good approach, but I don't know of any other way. Is there a way to directly bind Firebase to local storage?
The Firebase Local Emulator Suite is a set of advanced tools for developers looking to build and test apps locally using Cloud Firestore, Realtime Database, Cloud Storage for Firebase, Authentication, Firebase Hosting, Cloud Functions (beta), Pub/Sub (beta), and Firebase Extensions (beta).
Export your user data from Firebase. You can do this with the Firebase CLI: firebase auth:export users. json --format=JSON --project your_project_id . Massage the exported user data into a format acceptable to your new provider, using whatever data transformation tools you are comfortable with.
Firebase Storage - Firebase Storage lets you upload and store user generated content, such as files, and images. Firebase Authentication - Firebase helps you authenticate and manage users who access your application. Create and setup your account - Get started using Firebase for free.
I've decided that it is probably better to not be using AngularFire in this scenario, but rather to use the Firebase SDK directly. The 3 way binding of AngularFire makes it very difficult to intercept $scope changes to put into local storage. Using SDK directly allows you to intercept FB changes and save those directly to local storage as well as updating the $scope.
Similarly, making changes to FB using ref.update() is trivial and can be done alongside to updating local storage.
If the app starts in offline mode (detected from .firebasio.com/.info/connected) then simply use local storage, but also as Kato suggested "prime" the Firebase connection. I'm doing this by simply calling a blank ref.update({});
. Then when making changes to the data in local storage, also run your normal FB updates / push / save. Then when the app connects, the in-memory FB copy will sync with the server.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With