Offline persistance with cloud firestore in Android - android

I am using google cloud firestore to store data. There is an option to enable offline persistance. My requirement is to have offline support & also that data should be shown after kill & launch of app.
My Queries:
Can I use only cloud firestore & have pure offline support. [Lets say user entered some data, I push it to cloud firestore & then user kills & launches the app again this time without internet, so can that data be made available directly by cloud firestore or do I need some kind of room implementation]
I have set setPersistenceEnabled as true in firestore settings. Suppose user inserts something, which I try to push it to google firebase. But that time internet is not available & then user kills the app. When user again opens the app & internet is available so will that sync be automatically made just by cloud firestore or here again I need to apply some logic & persist data using room & push to firestore manually
Link for cloud firestore offline support documentation https://firebase.google.com/docs/firestore/manage-data/enable-offline

Yes, that's how it works. Data written while offline is persistent on the device. That's why it's called "offline persistence".
Yes, that's how it works. It will retry all writes until fully synchronized.
These are things you should be able to try on your own fairly easily.

Related

Android Application with firebase connectivty

If I create an android application which has firebase connection for database services, then is it necessary to have a internet connection to run and use the application?
The most important feature of Firebase Realtime Database is by far, the real-time feature. This means that you can stay in sync with the Firebase servers, to always get real-time updates. However, if there is no internet connection on the user's device, Firebase provides another very useful feature which is offline persistence. As in the official documentation, to enable this feature, you should simply use the following line of code:
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
But remember, this feature works for short to intermediate periods of being disconnected. If you want to use an offline-only database, you might consider using a local database. For that I recommend you using the Room Library.

What should I do to make firebase database sync even if the app is not opened

I read about firebase database persistence but I am not sure if it is the right way to make my application sync data automatically while it is not opened like other apps like whats app and facebook do. Is it enough to make the persistence enabled and the app will sync automatically even if it is closed or after system reboot or should I should use sth else like services and broadcast recievers
When talking about Realtime Database, persistence doesn't refer to automatic synchronization. It refers to local storage of data that has been previously synchronized, so that it can be used later while the app is offline.
Realtime Database will not do anything for your app when it's not open or running. If you want your Android app to do something when it's not actively being used, you will have to arrange for that yourself using tools such as JobScheduler and services. Then you can invoke Realtime Database to sync something as you wish.

Google Cloud Firestore Sync Devices Data in Local Network (no internet temporally)

Google has launched Google Cloud Firestore. Just as they says...
"Cloud Firestore is a flexible, scalable database for mobile, web, and server development from Firebase and Google Cloud Platform. Like Firebase Realtime Database, it keeps your data in sync across client apps through realtime listeners and offers offline support for mobile and web".
The question is...
In the application I am thinking about, needs that everyone to have data perfectly sync between devices (web and mobile). Ok, Firestore looks perfect for that.
* But what if my application also needs to sync between devices in a local network. Let's suppose if the internet connection is gone (whatever reasons...)*.
Does anyone could give me a direction how could it be made using Google Cloud Firestore (I don't mean the code). I use node for web application and Android for mobile.
Cloud Firestore as well as Firebase Reatime Database supports offline data persistence. This feature caches a copy of the Cloud Firestore data that your app is actively using, so your app can access the data when the device is offline. So, if you are using:
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
It means that Firestore will create a local copy of your database on your device, which in turn means that you'll be able to query your the database even if you are offline. So every change that is made while you are offline, will be updated on Firebase servers once you are back online. Unfortunately this local copy of your database cannot exist elsewhere than on your device. It cannot be hosted in a local network. Also all CRUD operation can be done only on the database which are hosted on users device.
Another thing to note, is that once the Internet connectivity is reestablished, you'll receive the appropriate current server state. The Firebase client synchronizes that data with the Firebase servers and with other clients that are using the same database. So, as a conclusion, in order to keep all your devices from a local network synced, you need to have internet access.

Firebase Storage Sync api

I am trying to develop an app whereby I want to exchange files (video, images) within the cloud storage(firebase) and client(android app).
I wanted to know if there is any sync API in firebase which keeps track of any updates i.e any changes being performed in the firebase storage and replicating the same to the client (and vice-versa if possible).
Thanks in advance !!
Go to the page of Firebase and read what the header says:
Store and sync data in real time
Furthermore:
The Firebase Realtime Database is a cloud-hosted NoSQL database that
lets you store and sync data between your users in real-time.
What Realtime means is this:
Instead of typical HTTP requests, the Firebase Realtime Database uses
data synchronization—every time data changes, any connected device
receives that update within milliseconds. Provide collaborative and
immersive experiences without thinking about networking code.
So just add Realtime Database Dependency and you are set. This itself will serve the purpose of Sync API you need.

Firebase instead of SQLite

I have been using SQLite as my storage solution for my android applications. I want to be able add synchronization functionality to one of my apps, Firebase looks like a good solution but the problem is that I need to know if I am to use Firebase to sync data with the SQLite database or Firebase can work and totally replace SQLite. I know Firebase has offline persistence but while offline can it hold as much data as SQLite and are the queries as powerful?
Clearly this depends on the business rules for which the options are considered. So there will not be the "right" answer to this.
At least one of our team members was thinking along these lines. Here is our solution:
Do all transactional data locally on the SQLite as we do not need transactions to be across devices.
Sync the rest using Firebase.
(1) makes sure that we don't misuse Firebase's non-persistent offline availability of its client.
The docs
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.
(2) make sure that we use Firebase's persistent offline nature to synch non-transactional data across devices once connection is (re)established.
The docs, The Firebase Realtime Database client automatically keeps a queue of all write operations that are performed while your app is offline. When persistence is enabled, this queue is also persisted to disk so all of your writes are available when the user or operating system restarts the app. When the app regains connectivity, all of the operations are sent to the Firebase Realtime Database server.
This way we achieve a state acceptable within our use.
"can it hold as much data as SQLite and are the queries as powerful?" no and no if powerful is translated as "transaction across devices", by definition of Firebase being a remote database.

Categories

Resources