If I have Ionic 4 app that uses the firebase JS SDK (AngurlarFire5) with firestore's offline persistence enabled, what would happen if there are pending requests and the app get closed either by sending it to background or completely closing it? Would it sync when re-opening the app or the pending requests/data will be lost?
The way offline persistence works on all platforms is like this. When data is written, the write is committed to local storage before it's synchronized to the server. Eventually, when the app is running and it's able to connecto to Firestore again, the writes will be synchronized. Killing the app doesn't change this behavior. It just delays the sync until the app is launched again and is able to connect.
Related
To use a firestore bundle, does the client need to have persistence enabled for the offline cache. If persistence is enabled, does it mean that the offline cache and the bundle are retained when the app or browser is closed and don't have to be re-downloaded when the app or browser window starts up again.
According to the firebase website, for the web, offline persistence is supported only by chrome, safari and firefox. Is this information up to date - is it possible that Edge, Opera and Brave browsers support persistent cache.
For the web, if cache persistence isn't available, is it possible to cache the firestore bundle locally some other way?
If the app requests to ead a document from the local cache and it's not there, will the document be read from the cloud if the device is online?
When you enable disk persistence, your app writes the data locally to the device so your app can maintain state while offline, even if the user or operating system restarts the app.
By enabling persistence, any data that the Firebase Realtime Database client would sync while online persists to disk and is available offline, even when the user or operating system restarts the app. This means your app works as it would online by using the local data stored in the cache. Listener callbacks will continue to fire for local updates.
Check the section here for more information on Handling Transactions Offline
Even with persistence enabled, transactions are not persisted across app restarts. So you cannot rely on transactions done offline being committed to your Firebase Realtime Database. To provide the best user experience, your app should show that a transaction has not been saved into your Firebase Realtime Database yet, or make sure your app remembers them manually and executes them again after an app restart.
As per the official documentation ,Offline persistence is supported only in Android, Apple, and web apps.For the web version, offline persistence is supported only by the Chrome, Safari, and Firefox web browsers
For the web, offline persistence is disabled by default. To enable persistence, call the enablePersistence method.
While network access is disabled, all snapshot listeners and document requests retrieve results from the cache. Write operations are queued until network access is re-enabled.
Also , check the following for similar implementations examples:
Firebase Offline persistence Upfront Caching
Firebase Offline possibilities
What happens when an offline device goes online
I came across a wonderful feature of Firebase offline feature. I integrated that in my app just by writing one line of code in my main.dart file after initializing Firebase await FirebaseDatabase.instance.setPersistenceEnabled(true);
Question 1 :
I couldn't able to understand the database.keepSynced(true) function because without using this line of code, my app is persisting old as well as fetching new updated data, so what this exactly does ?
Question 2 :
How could I prevent the write operations when a user is offline, because I read that after setting persistence enabled, it makes a queues of write operations and update them when user gets online, so how could I stop this ?
Question 3 :
Is this persistence feature going to work in IOS device as well or need some permission settings first ?
Thanks
When you call FirebaseDatabase.instance.setPersistenceEnabled(true) you're allowing Firebase to create a local file on the device where it persists any data it's recently read, and all writes that are pending while the device is offline.
When you call keepSynced(true) on a node, you are telling the SDK to always keep that node synchronized. It essentially creates a onValue listener on the node without any handler code, so you're purely doing this to keep the data synchronized for when the device does go offline.
By combining keepSynced(true) with setPersistenceEnabled(true), you're specifying that you want the app to continue working when it's offline across restarts, and which data is needed for that.
If you call keepSynced(true) on the root of your database, you're telling the SDK to synchronize all data in the database to the disk cache. While this may initially be a quick way to get offline mode for your app working, it typically won't scale when you more people start using your app.
If you only want to allow write operations while the client has a connection to the database backend, you can register a local listener to the .info/connected node, which is a true value when there is a connection and false otherwise.
Note that Firebase doesn't require this, as it queues the pending writes and executes them when the connection is restored. In general, I'd recommend working with the system here instead of against it, and also trying to make your app work gracefully in the offline scenario. In many cases there is no need to disable functionality while the app is offline.
Offline disk persistence is available on Android and iOS, but not on web.
I am a newbie to mobile development and I am building a mobile game in react native for android devices where I want to store user centric information such as score for different levels in realtime database in firebase. My users would be in playing game in restricted internet connectivity(where user connects to internet once in few days) and I want a mechanism such that app should store data locally even when the application "RESTARTS" or the app is killed by user and when internet connectivity comes, should be able to push all data on server.
I have two questions:
Can I some how automatically send data to server without user opening the app when the internet connectivity is established?
I found support for enabling offline data persistence in case of application or operating system restart for android in Java and kotlin
(https://firebase.google.com/docs/database/android/offline-capabilities), but did not find support for react native. I have gone through documentation of React Native Firebase library (https://rnfirebase.io/docs/v5.x.x/database/reference/database), but did find option to enable data persistence option for case when application/os restarts. Is there any workaround for this?
What you are looking for is Cloud Firestore, it has data persistence built-in so you won't have to worry. Use react-native-firebase for that, it has an absolutely beautiful documentation and guides and the community is very helpful too.
To sync your local data with the server without user opening app, you'll need to delve into the native side and make an Android Service that runs in the background and checks for internet connectivity regularly. As soon as the internet is connected, it can start the Cloud Firestore sync.
Do not use Realtime Database for this. Use Cloud Firestore which is way better.
I'm developing a Firebase based application using Ionic2 and Angularfire2. Does anybody know if its possible to setPersistenceEnabled(true) to allow for offline application use?
All Firebase SDKs handle intermittent connectivity loss, by keeping a cache of active data in memory and keeping a queue of pending writes.
Native mobile SDKs (for iOS and Android) have the ability to persist the cache and queue to disk, so that it survives app restarts. The web SDK does not have this ability.
I am having a problem under the following circumstances:
1. Running a webapp with both firebase sdk and angularfire initiated connections
2. App has been running in background. Meaning user has launched other apps. During that time, internet connectivity could have come and gone.
3. User brings webapp back into foreground as active app. Firebase connectivity via angularfire has been lost and updates have been lost as well. Hence "2way" binds to existing views/controllers are not longer current and will not re-establish/sync. Only recovery is to actively kill the app and restart.
This is obviously undesirable. I have currently attempted a bruit force method where I am issuing a goOnline() call at the beginning of each of my controllers in an attempt to always try to re-establish a connection but for services that expect a 2way bind, I am not sure that everything will sync up?
Any thought or guidance on this would be very helpful as this is a serious issue.