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.
Related
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.
I wanted to store the specific path in Firebase database (JSON) in local phone storage, not all data at the realtime-Firebase database. Say I have a news-feed path for each user in my Firebase-database and I want to save only the news-feed which is specified for the user instead of loading whole useless data on the local storage of user's mobile.
I read this paragraph from the official Firabase site which mentioned that
The Firebase Real-time Database synchronizes and stores a local copy of the data for active listeners. In addition, you can keep specific locations in sync.
DatabaseReference scoresRef = FirebaseDatabase.getInstance().getReference("scores");
scoresRef.keepSynced(true);
If this is the solution to my question, then I can use this line directly without writing that line
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
Those two bits of code are not really related. They don't do the same thing.
keepSynced(true) effectively keeps a listener active on the given reference, from the moment it's called, for as long as the app is running, so that the local version of the data is always in sync with the remote version on the server.
setPersistenceEnabled(true) just activates local caching of the data read by the SDK. When persistence is enabled, the app can still query data previously read. It takes effect for all data read by the SDK. While persistence is enabled, you can't control which data is cached - all read data is cached up to 10MB max. When the max is reached the oldest data will be evicted from the cache.
According to me the best way will be to parse the Firebase JSON data and map it into an object and then save only data that you need in a local storage (News-feed in your case), and then access it later whenever you need it.
For this, you can use Paper DB as a local storage to store specific data and use it whenever you need to. It stores data as a key value pair so you can access your data with the same key you inserted it with in the database. (Just like shared preferences work).
It stores data as cache in your local storage and uses Kryo serialization framework which is pretty fast for I/O operations.
Or you can also use Room (a google library) with SQLite to achieve this task.
Haven't tried Room but i think it will suite your purpose.
Here's the official documentation for Room
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.
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.
I'm using Firebase data persistence in my app. When I logout of the app, I want to clear the Firebase cached data as well. The persisted data is hampering the behavior of my app.
From this question, I understand that it needs to be worked around. If Firebase stored data in SQLite, how can I delete this SQLite file programmatically?