Reading some of data (not all) from a Firebase database node - java

I'm working on project in which I need to read the firebase node in this manner:
Suppose I have a node messages in a firebase realtime database. In that node, I have 20, 000 messages.
When a user enter in the message box, I need to show messages. But Its too time consuming to read all 20, 000 messages at a time.
What I want it, to read 30-40 messages at a time and show those to the user.
When user press more message button, I will read another 30-40 messages and show them to users. and so on.... it would faster and convenient.
But I can't find any way to make the things work like this.
I tried with ValueEventListener(), but it reads all the data in that messages node and it's time consuming. I also tried reference.limitToFirst(30) but it read first 30 messages, then what do I do? How can I read next 30 messages?
Could I have present my problem clearly? :(
Thanks in advance.

But it's too time-consuming to read all 20,000 messages at a time.
20,000 messages can be considered a lot of data. So I strongly recommend against loading such an amount of data in one go.
What I want is, to read 30-40 messages at a time and show those to the user. When the user presses more message button, I will read another 30-40 messages and show them to users. and so on.... it would faster and more convenient.
What you're looking for is called pagination. This means that you need to load the data progressively in smaller chunks. You need to load a group of 30-40 messages, one after another. This was already covered a lot of times before, and this question already has a lot of solutions for the Realtime Database:
https://stackoverflow.com/search?q=Firebase+Realtime+Database+Pagination+Android
I also tried reference.limitToFirst(30) but it read the first 30 messages, then what do I do? How can I read the next 30 messages?
I recommend you have a look at some of those questions and give it a try. If you have a hard time implementing it, then show us an MCVE with what you have tried, and this way, I and other Firebase developers can help you.
However, if you consider at some point in time trying using Cloud Firestore, I think that this answer will help:
How to paginate Firestore with Android?
If you want to go further and try using Jetpack Compose, then I think that this resource will help:
How to implement pagination in Firestore using Jetpack Compose?

Related

Overcoming a Very Long JSON String?

I am trying to build a weekly schedule, and I want to make an Android app to display it.
I have to send a Week object that contains a list of days, which contains a list of hours, which contains a list of events.
Every event has multiple variables.
The data comes from a local database I built on my PC, and on the app I set a GUI friendly query which returns the planned week in a JSON format.
Now that JSON has become 2000 lines long on the best case, and around 5000 lines worst case.
Any suggestions on how I can handle this?
Another problem is that the JSON comes back in packets, so I cannot parse parts on it until I receive it fully, and Java cannot handle such long strings.
I would suggest you to use Firebase as your database. Firebase is a cloud database which can be accessed by web and android app also. The data is kept synced at all times so you do not need to fire a different query in your app.
Just store the data in Firebase from your pc and access it via android app and display it.
Hope this helps.

How can I load more items when scrolling down in listview?

First I want to say: This question is no duplicate!
I already read a lot of questions about loading more items when scrolling down.
This was most helpful for me.
But all questions I read aren't explaining the basic principle.
So, what I mean is:
My app gets data from json and displays it in a ListView, but it's not possible to load all items from database with one request. The app crashes…
The solution is to load only 10 items and on scrolling down 10 items again.
But what is the basic principle to do this?
I thought about these 2 different options:
Set a LIMIT in my PHP file and send for each 10 items a new request from android and set LIMIT +10.
Send one request from android and getting all data from json, but only displaying 10 items.
Code isn't necessary, because I want to know the principle of doing this.
The approach I use in my apps is:
Step # 1: I load first set of data with limit from server and then store the last record data id in a variable.
Step # 2: Implement the on scroll end listener to your listView or RecyclerView.
Step # 3: Start another request inside on scroll end listener and load new records.(Here I set again data id in a variable)
For Example:
Start the request when the activity starts and do what I explained earlier.
GetBusinesses("&isOpen=2&cat="+Prefrences.getSelectedCategoryId());
Then inside your on Scroll End Listener
GetBusinesses("&isOpen=2&cat="+Prefrences.getSelectedCategoryId()+"&limit=10&lastDataId="+BusinessItems[index].mBusinessId)
Edit To avoid duplicate api call inside GetBusinesses() check if the request was started previously, well the idea is create a boolean initially false and then in your GetBusinesses() function make it true before starting the request and once the data is loaded and request is finish make it false again.
I hope this help.
Usually you would load 10 new items from the server each time. Use a page-parameter to identify which 10 items you need and where to place them.
loading all items at once could be way too expensive: The delay could be long and the user's data-plan won't be happy either. Obviously depending on how many items there are and what contents they have.
You will have to find the trade-off. Based on your data size, sometimes it makes sense to parse and save it all in local database.
Just for 200-300 records, you don't want to make another api call after every 50 records in list. Remember with mobile app user scrolls up and down very often. You might be unnecessarily sending multiple requests to your server, which might be an overload(depending on user count).
If you go with option 2, you can make use of something like JobIntentService to silently fetch data and save locally.
This approach will also let your user interact with no internet(offline mode) scenarios.

How to clear unwanted records in Kinvey Database

I am having an Android app and I am planning to use Kinvey Database to store some data.
One of the record in the entry would be having the last used time period.
The last used time period will be set by my app when ever the app is opened.
What basically I am trying to achieve is to run a code at the end of each month and clear all the record whos value of the last used period is more than 10 days.
Can any one please tell me whether it is possible to do this?
The reason for doing this is to use the least Server storage space as they provide only 1 GB/Monthly in the free plan.
What I understand from this...
What basically I am trying to achieve is to run a code at the end of each month and clear all the record whos value of the last used period is more than 10 days.
Can any one please tell me whether it is possible to do this?
... is that you would like to have scheduled code, which will be executed once a month. Please take a further look at the Scheduled Code feature of Kinvey.
https://devcenter.kinvey.com/html5/tutorials/scheduled-code-getting-started
Edit: more information on the topic...
Kinvey Scheduled code allows you to execute one of your custom endpoints on a specific date in the future. It is commonly used to
Aggregate, archive, and cleanup data.
Pull data from a third-party API into Kinvey.
Send out a batch of e-mails or push notifications.
I would not bother describing step-by-step initializing of Scheduled Code, since those steps may change in the future. Please follow the steps from the link above, those should be fine to get you further.

User matching with current data

I have a database full of two different types of users (Mentors and Mentees), whereby I want the second group (Mentees) to be able to "search" for people in the first group (Mentors) who match their profile. Mentors and Mentees can both go in and change items in their profile at any point in time.
Currently, I am using Apache Mahout for the user matching (recommender.mostSimilarIDs()). The problem I'm running into is that I have to reload the user data every single time anyone searches. By itself, this doesn't take that long, but when Mahout processes the data it seems to take a very long time (14 minutes for 3000 Mentors and 3000 Mentees). After processing, matching takes mere seconds. I also get the same INFO message over and over again while it's processing ("Processed 2248 users"), while looking at the code shows that the message should only be outputted every 10000 users.
I'm using the GenericUserBasedRecommender and the GenericDataModel, along with the NearestNUserNeighborhood, AveragingPreferenceInferrer and PearsonCorrelationSimilarity. I load mentors from the database, add the mentee to the list of POJOs and convert them to a FastByIDMap to give to the DataModel.
Is there a better way to be doing this? The product owner needs the data to be current for every search.
(I'm the author.)
You shouldn't need to ask it to reload the data every time, why's that?
14 minutes sounds way, way too long to load such a small amount of data too, something's wrong. You might follow up with more info at user#mahout.apache.org.
You are seeing log messages from a DataModel, which you can disable in your logging system of choice. It prints one final count. This is nothing to worry about.
I would advise you against using a PreferenceInferrer unless you absolutely know you want it. Do you actually have ratings here? I might suggest LogLikelihoodSimilarity if not.

Show users a list of unique items on Java Google App Engine

I've been going round in circles with what must be a very simple challenge but I want to do it the most efficient way from the start. So, I've watched Brett Slatkin's Google IO videos (2008 & 2009) about building scalable apps including http://www.youtube.com/watch?v=AgaL6NGpkB8 and read the docs but as a n00b, I'm still not sure.
I'm trying to build an app on GAEJ similar to the original 'hotornot' where a user is presented with an item which they rate. Once they rate it, they are presented with another one which they haven't seen before.
My question is this; is it most efficient to do a query up front to grab x items (say 100) and put them in a list (stored in memcache?) or is it better to simply make a query for a new item after each rating.
To keep track of the items a user has seen, I'm planning to keep those items' keys in a list property of the user's entity. Does that sound sensible?
I've really got myself confused about this so any help would be much appreciated.
I would personally do something like:
When a user logs in, create a list of 100 random IDs that they have not seen. Then as they click to the next item, do a query to the datastore and pull back the one at the front of the list.
If this ends up too slow you can try to cache, but it is really hard to memcache you entire database. Even loading the 100 guys they need will be hard (as the number of users scale out). Pulling back 1 entry for 1 webpage load is not slow. Each click will be post 1 comment and pull 1 item back. Simple, only a few MS from the datastore. Doing the 100 random IDs they haven't seen can be slow, so that makes sense to do ahead of time and keep around (in their request or session depending on how you are doing that...)

Categories