I am using the firebase realtime database in my android app. I only want the last entry inserted into the database and not an entire database sync.I am doing the following.
FirebaseDatabase.getInstance().getReference("reference").limitToLast(1).addValueEventListener(this);
Here's what I could not clearly understand :
1.When I do a limitToLast and add a listener, does it download the entire data from the database into the local copy and pass me a snapshot of the last entry or does it download only the last entry?
2.When I remove the listener , does the sync stop between the realtime database and the local data or the sync keeps happening but the onDataChange is not called?
3.The difference between removing the Value Event listener and going Offline.
When you call limitToLast() you create a Firebase Database query. Such a query only synchronizes the data that it matches, so in your example it only downloads the last item and invokes onDataChange with it. Then when somebody adds a new item, it invokes onDataChange again with the new last item.
Removing a listener stops the data synchronization.
Going offline temporarily stops data synchronization, until you call goOnline().
Related
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!
I'm trying to download data with Firebase. The issue is that I'm downloading the data based off of a timestamp. If their is only one item in my ListView that I'm populating, I want to get the next two hours worth of data from Firebase and if there isn't any data from the last two hours, I want to get the next two hours of data and so on until I get some additional data. The issue is that I don't exactly know how to determine if Firebase actually downloaded data. Because if data was downloaded, I can use an OnScrollListener for my listView to download more data since it will be scrollable after that point. Any help would be appreciated.
You cannot know when getting the data from the database is completed becase Firebase is a real-time database and getting data might never complete. That's why is named a realtime database because in any momemnt the database can be changed, items can be added or deleted.
The only way to partially know if you have all the data at a particular location is to perform a single 'value' type query on it. Even then, the data may change after that listener is invoked, so all you really have is a snapshot at a particular moment in time.
You can use a CompletionListener only when you write or update data in your database and you'll be notified when the operation has been acknowledged by the Firebase servers but you cannot use this interface when reading data.
in my Android app, I have a List of objects (Employees) obtained from a JSON on a server. Whenever I open the app, it parses the JSON, creates the ArrayList and populates RecyclerView. I would like to create a database and load the data from the database instead of JSON (so that it works offline as well and is faster).
My question is: how do I keep the database updated to make sure I have the same data in JSON and database?
What is the best approach to do this? I notice when I open the gmail app for example, it loads my emails even when I am offline, so it must be using a database as well.
EDIT: Solved it with using DBFlow and just calling save() on the whole list, which automatically decides whether to update a row or insert new one.
Scenario "DB is empty": Show empty view message and fetch data from API endpoint -> save that to database and afterwards populate RecyclerView from database.
Scenario "DB not empty": Populate from database -> try to fetch new data, if there is no connection it still shows old data otherwise do steps from first scenario.
Additionally you could compare the custom class that populates the RecyclerView to prevent glitches while reloading same data.
This question already has answers here:
Firebase on app startup taking more than 3 seconds to load data
(2 answers)
Closed 6 years ago.
I have integrated Firebase Database.I want to fetch database data on opening a particular activity.
As per documentation-
The value event is used to read a static snapshot of the contents at a
given path, as they existed at the time of the event. It is triggered
once with the initial data and again every time the data changes.
The onChildAdded event is typically used when retrieving a list of
items in the Firebase database. Unlike the value event which returns
the entire contents of the location, the onChildAdded event is
triggered once for each existing child and then again every time a new
child is added to the specified path.
As per my understanding both addValueEventListener and addChildEventListener can be used to retrieve data and it should get called once everytime listener is attached and then whenever there is a change in data.I tested them one by one by adding listener inside onCreate method.I noticed both of them do get called after activity startup but with a delay of 15-20 seconds.Is this delay normal?Is there anyway i can immediately retrieve content from firebase database on Activity startup?
The delay you are seeing is likely down to retrieving it from the Firebase servers.
https://firebase.google.com/docs/database/android/offline-capabilities
Use offline data to speed this up and keep the important data synced locally for quicker access.
I'm making an app which has to download some data, parse it and store it in a SQLite Database. However I'm having a problem where the downloadtask (an asynctask) executes everytime the app is launched and keeps appending the duplicate data to the database so I get multiple instances of the same data.
I only want to execute the download task if the database has values in it but still want to be able to run the downloadtask if the data file on the server is updated.
Other than checking if the number of rows in the table is greater than 0 how would I go about doing this? I'm not really sure what to search for. Any help would be appreciated!
Each reach should have a unique ID.
When loading the data check if the unique ID is in the database
If it exists update the row.
If it doesnt exist add it(append).
If you control the database in the website, you can put there a flag you update anytime the database is updated. So before reading the data, check your flag. If updated load new and append new data to the database.
For your local part of the app: Well you can easily update the data as you desires if a query on this database return more than one row. This is very easy using a cursor and getting its count after the query is executed. If you dont want to have duplicates just delete the database after you have made sure that your download task has retrieved the data from the remote source. In that way you won't have duplicates and you will be sure that you don't delete by accident the database if something goes wrong.
For the remote source:You can enable Push Notifications and send one after something is changed in the database.So by sending a notification you can trigger a service that will download the new data and parse it properly.