Firebase: How to Query Local Database Even if Device is Online - android

FirebaseDatabase.getInstance().setPersistenceEnabled(true);
How to change behavior of firebase querying for server database or locale database.
DatabaseReference scoresRef = FirebaseDatabase.getInstance().getReference("scores");
scoresRef.keepSynced(false);
Is that possible to make same query for server and than used it from local database. firstly I want to make a query from server database and then without download again and again I want to use same querying datas from local datas even after app is closed.
I am using above codes for using offline firebase and trying to use same datas without download again

When you are using the following line of code:
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
It means that Firebase will create a local copy of the database on user device. This means that the application will work even if the device temporarily loses its network connection or if the user restarts the application.
So the answer your question is, that the same query that works when you are online will also work when you are offile, because the database which will be queried is the local database. You don't to do other operations.

Related

Does Firebase charge queries done on database downloaded on the device using disk persistence?

I am using Firebase rtdb. I want to download my database for offline capabilities. If there are no changes on the database, do I get charge to the queries done on local copy of realtime database? And if an update is done on the database, with offline capabilities enabled, is the whole database gets downloaded again or just the part that was changed?
Queries that only use locally cached data are not billed in any way. Only queries that actually use the Firebase cloud-based services are billed. The local cache is only synchronized with the pieces of data that actually changed, not the entire thing every time.

Not retrieving new data after setting Firebase Persistence Enabled to true?

After making FirebaseDatabase.getInstance().setPersistenceEnabled(true); When query to get data
Query query = FirebaseDatabase.getInstance()
.getReference()
.child("notifications")
.child("entities")
.child(FirebaseAuth.getInstance().getCurrentUser().getUid().toString())
.orderByChild("seen")
.equalTo(0);
From data tree
it gives old data that is in cache instead of new changed data in Firebase databse.But When I change setPersistenceEnabled(true) to setPersistenceEnabled(false) then everything is fine While I also want firebase data to be persisted in cache and also change When change is made to Firebase database.Please help to maintain Firebase data persistence along with realtime change.
If you are using:
setPersistenceEnabled(true);
It means that you'll be able to query your the database even if you are offline. This is happening because Firebase creates a local copy of your database that will persist after your app/device restarts. Every change that is made while you are offline, will be updated on Firebase servers once you are back online. To be more clear, every client that is using a Firebase database and uses setPersistenceEnabled(true) maintains it's own internal (local) version of the database. When data is updated, it is first written to this local version of the database.
So, 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 that your app will work as it would be online by using the local data stored in the cache.
But, there is no way to stop the retrieval of the data from the cache while you are not connected to the server, as you cannot force the retrieval of the data from the cache while you're connected to the server and unfortunately this behaviour cannot be changed.

Link Android Room Database with Firebase Realtime Database

I am using Android Room Database for creating the database for my android app. It works perfectly for me locally but I cannot link to a server to have it online as well.
I am using Firebase for Authentication. I am trying to use Firebase Realtime Database to save the whole database object from Room and load the correct database on app startup according to the authenticated user.
I want to ask if this is possible at all? and if I can just save a whole instance of Room database or I need to re-create the database on Firebase and save my data item by item?
I also can't seem to be able to get access to the database data of Room, as when I get an object of the AppDatabase class it doesn't really pass the data. And I don't know how should do the opposite, to assign the data retrieved from Firebase later to the local data saved?
Also if it's not possible with Firebase, do you have any recommendation for some other server I can use with Room?
After a lot of researches and looking desperately for an answer here's what I reached:
Firebase already got a straight forward way to create the database and host it online. (I had my database already created so was trying to save time, but creating it from scratch using Firebase Realtime Database was a lot faster)
Room Database is quite perfect if you are planning to save your database locally and offline (Up to the post date)
I think their is no need to connect your Room Database with Firebase Realtime database because,okay first try to solve this problem, why we use Room Database ?because when we don't have internet, we can get data from Room Database , no need of network... Firebase also provides offline mode , it can also save data in persistence, so why we use Room Database ?.. Hope you got the point...
Similar project created by me which uses
Firebase authentication to login user
Save and cache user notes to sql lite database with Room
Save user notes to firebase base database
✍️ Simple Note Making App use Sqllite Room 🧰 for caching the notes and 📥 Firebase Database for online storage
https://github.com/LanguageXX/Simple-Note-App-with-Online-Storage
Hope that helps you

Ways to have synced offline working data in Android and iOS app

I have an android application which uses SQLite database (stored in APK) for storing offline working data. The app is used only in offline mode.
Now I want to create iOS version and I want to implement idea of synced databases on both platforms - Change database on server and it will update databases in device when will be online
My first idea was to replace SQLite to Firebase Realtime database but there is a problem with offline mode. Is there a way to cache the whole JSON for offline use?
Or is it easier to use a cloud for storing SQLite database and implement syncing with new versions of the database?
Thank you for your responses.
The Firebase Realtime Database client automatically downloads the data at these locations and keeps it in sync even if the reference has no active listeners. You can turn synchronization back off with the following line of code.
DatabaseReference scoresRef = FirebaseDatabase.getInstance().getReference("scores");
scoresRef.keepSynced(true);
scoreRef.setPersistedEnabled();
But this need very first internet connection to sync, this will no enable data without that first request.
For detail about the offline capabilities, head out to the official doc link here.

Can I use Firebase for applications that has just database operations?

I am working on a budget management application, which needs to store users incomes and expenses then show back to user. I am using SQLite in my project which takes lots of code to save and retrieve data. I wonder whether I can use Firebase just to save and retrieve data. Any answer is appreciated.
Yes, sure you can! Just just use the Firebase Realtime Database service. Think of it like a JSON-ish storage in the cloud :-)
As a bonus, you can enable offline persistence and query your data even offline:
Firebase applications work even if your app temporarily loses its network connection. In addition, Firebase provides tools for persisting data locally, managing presence, and handling latency.
The Firebase Realtime Database stores data returned from a query for use when offline. For queries constructed while offline, the Firebase Realtime Database continues to work for previously loaded data. If the requested data hasn't loaded, the Firebase Realtime Database loads data from the local cache. When network connectivity is available again, the data loads and will reflect the query.
You can enable disk persistence with just one line of code:
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
Firebase database writes, while offline, would be added to a queue which would sync once network is back and online.

Categories

Resources