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.
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 want to understand how can firebase store object data for later delivery when the target devices has no internet connection. Because i want to create my one method that store a list of string when there is no connection and send them right after the connection is available.
Firebase has two databases these days: the Firebase Realtime Database, and Cloud Firestore. Luckily for the scope of your question their behavior is quite similar, but you'll typically want to specify which database you're asking about in your question.
When you make a change to the data in your app, Firebase does two things:
It updates its local snapshot of the data, and firebase local events for the change.
It sends the change to the server, to commit it to the permanent storage there.
If your app is not connected to the server, it puts the change in a queue of so-called pending writes. If you have disk persistence enabled that queue is written to disk, but otherwise it just exists in memory.
When the app is running and has a network connection, Firebase sends any pending writes to the server.
You can use a Sqite for store data then if internet available then sent data to firebase. Need some basic logical program.
Create Database in your application and make one class which save data in your database
.when the user went to save data in firebase,checking connection if no save data in firebase and the first time that user open app with connection try to save it in firebase and delete data from data base
look more information at this doc
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 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.