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

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.

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.

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

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.

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.

Firebase Database offline writing

In my Android app I have to keep an existing SQLite database, but in order to sync elements (tasks from a todo-list) to multiple devices I decided to use Firebase Database.
So I'm not enabling Firebase Database offline mode, since I have to use SQLite for that (it's for a college project).
So what I'm doing for writes is that, whenever some data gets added or removed from the local SQLite database that change is also made to the Firebase one.
All this works as expected if the user is connected to the Internet, or even if the user adds a task offline and then becomes online while being on the app.
But if the user adds a task offline, then closes the app and then becomes online in another app, the change doesn't get pushed to the database, even if the user opens my app again.
Any ideas?
Thank you.
As you did not enable the Firebase Database offline mode all data your app trying to push are not saved in persistent storage, only in memory. Thats why all these data are not pushed to Firebase after application restart.
So you need some synchronization algorithm at application startup which pushes data from you local database to firebase (and vise versa).
For example, you can use timestamp marker (System.currentTimeMillis()) in both local and firebase side which always should be updated on any database change.
Then at application startup you can just check this marker:
if local marker is greater than the remote one - that means that
remote database is outdated. So just copy all your local sql database
data to firebase.
if remote marker is greater than the local one - just clean local db
and download all data from firebase.
if both markers are equal - that means that everything is up to date,
do nothing.
Hope this helps :)

Firebase Realtime Database on Android - Persistence

I need a clarification about enabling persistence in Android for Firebase Realtime Database. Docs says:
"With disk persistence enabled, our synced data and writes will be
persisted to disk across app restarts and our app should work
seamlessly in offline situations."
so my question is: after an app restarts, how can I retrieve data from persistence without making another query?
For example if I save the last record key, I can do the query as
ref.orderByKey().startAt(LAST_KEY)
but the problem is that i cannot retrieve previously saved data.
Should I use a local database to store data?
Thanks
.keepSynced(true);
it will sync your latest data up to 10 MB into the local data storage, even you restart the app data is already present in firebase local storage.

Categories

Resources