I am using Firebase Database Local Persistence:
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
And also using pagination FirebaseRecyclerPagingAdapter.
The issue I am facing is that new values added to Firebase Realtime Database is not been updated on RecyclerView, even if I restart listening to the database. I mean, I close and reopen the Activity where the listener is contained and I always get the old data.
It seems that I am always retrieving the local database instead of the updated data in the Firebase Realtime Database.
I would like to use both features.
1) Why am I always getting the old data?
2) Is it possible to use both features without getting this kind of issue? If yes, how?
I appreciate any help!
Related
We are using firebase realtime database and I was thinking about implementing a cache locally to reduce repeated calls.
So I came up with an algorithm which involves room persistence library.
stream only the latest data from firebase
store in room cache
when requested fetch all data from room cache and return
But then I started thinking about the cache that firebase provides and started realizing that I might be able to avoid room library at all.
stream only the latest data from firebase
when requested call fetch data using a singleValueListener
Since we are using single value listener only the cached data would be fetched from firebase.
What are the drawbacks of using the second approach? I know that firebase cache is limited to 10MB so that might be one
The one big thing you need to know here is that the cache managed by the SDK is almost fully outside your control. You can set the size of the cache, and you could clear it by trying to find the database file it uses, but otherwise, you can't configure it.
If you write code on your own, will have to make every decision about how it works, and it will be a lot of code to get everything right.
Unlike in Cloud Firestore, where offline persistence is enabled by default, in Firebase realtime database to enable offline capabilities you need to use the following line of code:
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
This means that you'll have by default a local copy of your database. So there is no need to add another one. So in this way you'll only get new data, otherwise everyhing is you get is from cache.
I am building an android application and I am facing some weird issues related to FIREBASE DATABASE.
I have created a firebase database and added some tables, fields and added some data, like;
Log in with phone authentications etc.
I can use this database with my android app, but I can't be able to see the database.
I have tried a lot to refresh again and again, but not working.
If someone is using firebase, then please login to the console. firebase and check if you can see it, might be, GOOGLE changes its UI (User Interface).
Help me, please!
It will be a great favor.
I think you are not selecting your project:
Check if that is your project, or change it.
If it's prompting you to create a database for that project, it is because that is not the project you are working on. You need to select the current project you are working on in order to see your database.
There are two options - 1. Realtime Database i.e. Firebase. 2. Cloud FireStore. You have to select 1 instaed of 2. See the image below
I was experiencing the same problem. I was worried that all the data might have been deleted. I just left the tab open and after few minutes the database and its children started showing. I think if your database contains a lot of data, it takes time to load the data.
You have to click on Create Database button and setup rules before you try to save data on database. Remember Your are only setting up rules here not giving any database name.
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 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
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?