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
Related
In android, I have a list generated by user input which needs to be compared to a list in Firebase Realtime Database. I want to compare if List 1 contains all the elements in List 2.
This data then needs to be displayed in FirebaseRecyclerPagingAdapter.
Is there anyway this can be achieved?
There is no way to compare two lists in a single operation on the Firebase Realtime Database (nor on Cloud Firestore for those wondering). You will have to load the relevant lists from the database and perform the comparison inside your application code.
I recently saw that google has a great library for paginating data.
In this Google IO:
Android Jetpack: manage infinite lists with RecyclerView and Paging (Google I/O '18)
They explained how to make Data + Network DataSource and grab data from database as Single Source of Truth and when the database is out of data it request for more data from the network with BoundaryCallback.
So assume that I have a list of Movies on the server. And client (Android user) can sort them by popularity, title, date_release and ...
So in the first time if the user sort movies by titles everything will work great because there is no data in database and data will be requested from the server to sort them by title and send them back.
But what if after that user tries to sort them by popularity for example? Because of the availability of chunk of movies in database (for example 50 movies), it will sort this small amount of movies by popularity and then tries to grab data from server and this is not a good experience.
I cant make table for every sort type because it is not a good practice. So how can I overcome to this problem?
Thanks very much
Your question makes sense. IMO you do not have to make tables for every sort types but you can keep a track of which endpoint the data came from. For example, you have three endpoints with query ?sort=default, ?sort=ratings, ?sort=released_date. You can add three extra Boolean attributes (say fromDefault, fromRatings, fromReleasedDate) to your db model class to keep track of which endpoint it came from & update those corresponding flag whenever same movie data arrives from multiple endpoints.
Now when you sort by rating, you will use SQL query to filter those where the flag fromRatings is true. But initially, you will have none so your BoundaryCallback fires up the server request, gets the ratings sorted movie data and before saving, you'll have to override the fromRatings to true.
Hope it helps.
So I have a database in Firebase Cloud Firestore that stores users and tasks. Each user has a number of fields such as name, username,etc. I want to know how to fetch each field that is associated with each user using Kotlin.
I'm trying to get all these accounts so I can display them in an activity called friends, that's why I want to collect all these accounts from the database. Here is a picture of my database to have a clear understanding of what I'm trying to do.
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.
I have a JSON query for 3000 items. At first launch my app, I have to retrieve this data and put it in SQLite, in next launches the app will get data from db or will ask api about update data. When app is putting data to db, it should show about twenty items in recyclerview with endless list and load more items, when the items are gonna finished. How implement this? I understand that I need to perform these operations in background threads, but how organize it. At first app will retrieve data from asynchronous retrofit 2 request and how then load this data in SQLite and show it in the same time without lags? Should I use contentprovider with cursorloader or something with ORM? Thanks for answers.
First of All You Should not load this much data at once. you should load that data chunk by chunk (Called Pagination), that mean your calling API also be capable for response to you chunk by chunk.
For your problem you might do is
Load your data from server
Use separate thread or service to handle the data storing into the SQLite.
Use Main Thread to represent the data.
Read this two blogs below, hope this will help you to understand how you can resolve your problem.
RecyclerView Pagination,
Sqlite from Service