Retrieving content immediately from firebase database on Activity startup [duplicate] - android

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.

Related

Is there any way to set TTL (expiration) for Room record in Android?

I'm trying to implement an offline cache using Room instead of OkHttp Cache. The cache result of each request is only valid for short time like 30 mins.
Here is the flow:
The app first load data from database
If the data is available and not expired, display it to user.
If not:
Load data from API
Refresh cache with new expiration time(or time stamp)
There is NO automated or an in-built way to handle the time expiration in Room Persistence Library(Room Database). One has to handle it manually.
One way to do that is create two columns in the table:
createdDate
lastUpDatedDate
Whenever there is an Insert or an Update operation, the lastUpDatedDate column should be updated with the current timestamp.
The next time a Read operation happens, limit it by the TTL(defined in Android Client) and whenever there is Write operation, update the TTL.
This is one way of doing it. Other ways can include implementing threads(or Couroutines, if Kotlin is used in code base).

Firebase asynchronous query - matching results

Hello fellow developers,
I have a question about getting the data from Firebase. Trying to compare the transactions list of one user with another user in a function. Two separate hashmaps are used to store the data (transaction list) of each user. Then I loop through the list of records of first user and try to match with the records of the second user. Since Firebase is asynchronous, function skips before I get the data. I need some ideas to manage the asynchronous fetching of data from Firebase.
Thanks

Determining if Firebase didn't download data with startAt and endAT

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.

Queries regarding firebase realtime database

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().

Android get data if data is one hour old from the Web Services

I have the android application where the webservices data is not frequently updated say once in a day.
I have to save data in DB for offline viewing as well, right now I am thinking that, when user clicks an button to get data it checks if the last data entered in DB is one hour old, if no then no downloading from web service if yes then download.
So, at the time I am saving data I will save current time with it , and calculate the difference and get latest data.
Is this a correct approach.
Asmi
Having a field in database table just for this purpose isn't an ideal approach. Instead, you should save the current time in SharedPreferences and compare the time to see if data needs to be refreshed.

Categories

Resources