display user according to there last chats in recyclerview - java

I have created a one to one personal chat application for my college project. As users chat with each other there chats are been stored in Firebase as in the following manner
+ Chats
+currentuser
+Reciverid
+postPushid
+informations
I want to display the users to whom the current user had a conversation according to there the last message means as the new message is been inserted under the current user chat node that users id should show on the top as according to there time of insertion.
Help me out to arrange according to user ids according to there last message time in Firebase.

Just add one more attribute as timestamp and generate it by using the command
long timestamp = System.currentTimeMillis();
save that to firebase when uploading the chat message with a negative sign like
timestamp = -timestamp;
Now in Query variable just add the this
query.orderByChild("timestamp");
this will select the value which is least but the value in positive becomes least in negative and that's how it gets resolved.

I already implemented the same logic. I did other model of chats. I have separate table with users. Separate table with chats where each user see only users he got chat with. And I have separate table for messages where id is the chat. So in this case it is very to manage and it is flexible. You can add/remove fields from each model separately. Want time stamp in messages no problem. Want timestamp in chats no problem. All tables maximum got two levels instead of five you got. So i would recommend think about restructure of db.

Related

How to sort leaderboard data in Firebase database for a specific date query?

I'm storing scoreboard along with date in milliseconds and query the date using startAt and endAt methods. And this is giving me all the users in that date range, now I want to sort the users based on score but I'm not able to do that.
Unfortunately, you cannot achieve this with Firebase realtime database without making some changes in the structure of your database. Unlike in Cloud Firestore, the realtime database doesn't have the capability to perform filtering on multiple properties (using "multiple WHERE clauses" as can be said in SQL terms).
If you want to check for matches on multiple properties, you'll have to create a composite property that can contain a combination of the properties that you are looking for. As an example, you'll need a string value that has the score and the date together like explained in my answer from this post.
This is a very common problem. As you've not attached you database, or your code, I can't give you the proper code of how to do this, but the best way to retrieve your users in the same order of their score can be done by a basic principle.
The basic principle is that in addition to setting the name and score in a user object, you can use setWithPriority to also set a priority for it.
Priority in this particular case can be the user's score (if its numeric, it will automatically be sorted for you). You can then use the .limit(10) query to get the list of top 10 users.
You'll also have to implement the child_added, child_changed and child_removed events to handle the cases of a new user entering the top 10, someone changing position and someone leaving the top 10 list respectively.
You can read and know more about this here: https://www.firebase.com/tutorial/#example/leaderboard

Can I run an infinite loop in my java program to check if a third party have done somechanges in the database

I am designing a program in java and using mysql as DB. The program allows multiple users to connect to the DB.
An important part of the program is instantly updating the users that another one has made a change in a specific table that will have the user do an action and hence the changes will be show on his end.
I thought of listening on a socket but I couldn't resolve the concept of multiple users and how the sender will notify a specific user only not the entire group
this is similar to text message
So what I am trying to do is run a while loop that runs every 10 seconds to enhance the performance a little bit and will check the table against the username I am using to log in
this will get me what I wan but I know it is not efficient
Any Ideas ???
Use the "version control" pattern in the database table. Add a numeric column that is incremented each time the table is updated, such as:
alter table users add version_number bigint:
Then, each time the row is updated, you should increment this value as well:
update users
set my_value = ...,
version_number = version_number + 1 -- include this change
where id = 1234
This way, each client will remember the last version_number they saw. I they find a different version number, then a change has happened so an action should be taken.

How to insert data into database in next row automatically

I am working on access control system with a database as a project. I want to the database to serve as a data log. That is, I am writing a program that writes the login time and logout time to the database and also logout date whenever access is granted to a user. What I am finding difficult is causing the cursor go to the next row whenever a user is inserted into the database(is logged in). Can anyone help me on how to use Java to get this done? I want each user to be inserted into database in separate row so that even if the same user logs in a day after, it goes to the next row not being updated or over written by new data log
Thanks guys, I have figured it out. All I have to do is auto increment the raw number each time data is written into the database. When data is sent to the serial port, it will be read as strings. the string of characters which represents users card number is also available in another separate database table. The read card number is compared with those in that database table. if match is found, Names of the user having the card number will be SELECTED and written to the log table. Each time there is data in the serial port the row number is incremented.

android, Everyone should evaluate each day only once

I'm going to build an app. Until now everything runs very well. Now I have a problem. The app gets its content from a mysql database.A column is called item.I have a ratingbar. The user can rate the item there.Every time the user evaluates an item the value is stored on the database in the respective item line.The values ​​are then added. In other words, when a user evaluates 20 times with 5 stars, the value adds up to 100 and so on.
I want to limit this. I will that the user can evaluate each day only once an item. I will it without a registration mask for the user. How can I solve this problem?
I know that i can identifier the WIFI MAC Adreess and other Unique Identifiers, but how can i solve this with them?
I can not use sqlite database, because the items should update with the time from the mysql database.
A registration mask should not be excluded. If this process is quite possible with them, then I supplement it with it.
I am looking forward to every comment
every computer has a machine ID, you will hash that and encrypt that to use as your identifier..most telecomms do not like using MAc addresses as IDs
One option would be to create UUID during every installation and sending this UUID to your server along with every request. In server, you can control very well if user can provide feedback once a day or others based on your requirement. Pls refer this link on how to create UUID.
https://developer.android.com/reference/java/util/UUID.html

Complexity of a query in the Google datastore

I have an Android app where users will be able to send private messages to each other. (for instance: A sends a message to B and C and the three of them may comment that message)
I use google app engine and the google datastore with Java. (framework Objectify)
I have created a Member entity and a Message entity which contains a ArrayList<String> field, representing the recipients'ids list. (that is to say the key field of the Member entity)
In order for a user to get all the messages where he is one of the recipients, I was planning on loading each Message entity on the datastore and then select them by checking if the ArrayList<String> field contains the user's id. However, considering there may be hundred of thousands messages stored, I was wondering if that is even possible and if that wouldn't take too much time?
The time to fetch results from the datastore only relates to the number of Entities retrieved, not to the total number of Entities stored because every query MUST use an index. That's exactly what makes the datastore so scalable.
You will have to limit the number of messages retrieved per call and use a Cursor to fetch the next batch. You can send the cursor over to the Android client by converting it to a websafe string, so the client can indicate the starting point for the next request.

Categories

Resources