Inserting The UID into Firebase Database Only once - java

In the main activity of Android Studio, immediately after anonymously signing in into firebase, I have this line of code:
FirebaseDatabase.getInstance().getReference().child("Online Users").push().setValue(currentUser.getUid().toString());
So, the UID of current user will be saved into firebase under the child "Online Users". Good. Problem is, when I open the app for the second or third time, or if I navigate from another activity to the main activity, this line of code gets executed again, and now I have several identical entries in the firebase. I mean, one UID has been pushed into firebase several times. But I don't want that. I want only one copy of each UID there. How can I achieve that?

Whenever you want a specific value to be unique in the Firebase Database, model it as the key of a list.
In your case that means that instead of calling push() you actually use the UID as the key:
FirebaseDatabase.getInstance().getReference().child("Online Users").child(currentUser.getUid().toString()).setValue(true);
Now whenever the user comes online again, they're just writing true to the same location again.
Note that the same user can come online from multiple devices. If you are trying to build a presence system, tracking what users are online, I recommend studying the sample presence system in the Firebase documentation which handles the edge cases correctly.

Related

Create a reaction system for post in an android app

I am developing a social network in android studio with the java programming language and in turn I use Firebase Firestore database as a database. I want to implement a system of reactions to the publications that are made in the app, but I can not find a way that when the user presses the reaction button, the reaction is saved in the corresponding publication.
Then I want that at the time of pressing the reaction button the value of the likes is updated to the correct publication
I made a small code but it only updates the value of the likes to the publication that I indicate by code
Here I pass the collection, the unique identifier of the publication and update the value of its likes
So instead of putting that instruction by code, I want the post ID to be automatically detected and able to react to any post and update its values.

How to release information stored in database after pausing activity for some time?

I'm building my Messenger app. There's table called "Loaded" in MySQL server, which basically stores a pair of VARCHAR: Who_requested - Who_loaded. It means that the user named "Who_requested" has loaded user named "Who_loaded" into their main chat page. So when "the main user" scrolls down to see more users below their chat page, the server will know what to load next (I'm implementing pagination in RecyclerView). It looks something like this:
The thing when user stops the app, I'd like to wait for like 15 minutes before releasing all information related to "the main user" in loaded table and restart the app (like the effects that you might notice in Youtube after putting Youtube in the background for a while). How to implement this?
Some methods are
One
The obvious way is to have a task/service running that prunes the out of date entries. The disadvantage is that this could die and never run again. So you have to have some way of restarting it.
Two
If you don't want to do the above, then another method is to use a trigger.
Have an AfterInsert Trigger on the table
Delete out-of-date entries
The advantage is that you don't need another task that could fail. The disadvantage is that if there are no new entries, then the table does not get pruned.
Three
When you fetch the entries to display/action, only fetch the ones that are newer than 15 minutes. This doesn't prune them, but everything works fine. And you use a task or trigger to delete them.

Best practice updating data based on user information with Firebase/Android?

How should I proceed to build or update data based on Firebase user information ?
I'm building an app where people can posts messages in an open feed. On screen, these posts and replies contain the username and profile picture. I'm using Firebase to store these posts and the users.
The question is should I "build" and store a post with the profile pic (url) and username stored, "hardcoded", in that post or retrieve data stored in the corresponding user subtree in Firebase for each post?
If a user updates his profile picture, the first solution means searching through all posts and updating every corresponding post and replies but is quicker in loading the feed, the second seems more efficient for a large number of posts and users but more laggy on execution.
This is my first time with Firebase and with Android. What is the best practice ?
The common practice is to store the profile picture in Firebase Storage. When uploading the picture you need to get the coresponding url of that picture. Having that url, you can easily store it in the Firebase Database or in Cloud Firestore. Every time you need to display the picture, just simply use the reference of that picture. If a user will change the profie picture, there will be no need for doing something. Using that reference will display in every place the new picture.

Firebase chat app: query messages, optimize query that should be OR, or improve NoSQL db structure

I'm developing an app on Android using Firebase that should embed a chat.
My issue is that to fetch messages, I need to query all the messages which have my uid either in sender or receiver field. This would be extremely easy to do in MySQL, but in Firebase (I must stick onto Firebase) looks kinda pain.
I cannot just filter them like that. And being receiver and sender fields of the object inside chat, I cannot even just filter them when I'm using a Firebase url like firebase.myapp.io/chat.
So the only possible solution in this model, is fetching all the chats and client-filtering them. This is all but good way to do this job. Also, when the message will be many, if they should, everything could become extremely slow.
So I thought about different ways to achieve the result:
I get in chat the keys corresponding to user uid. Among the values, I get chats from user point of view, or I got the keys of my receivers as values, and inside the messages
But I don't like this very much since it can be extremely redundant, since every message should be inserted twice in the db.
Another way would be memorizing messages keys into another object, like chatmessages, and inside I got users uids as values, and as keys containing chat message keys.
What would be the best NoSQL way to manage multiple, private chat conversations?
I currently have the same problem with a chat application am creating. I recently found a solution:
1) I basically used the two users unique ID and converted them to a number using ID.getBytes().
2)Then I added the 2 numbers one to the other (addition) and converted them to a base64 number again.
3) Used the resulting number as the id for the chatroom.
It worked but since am doing all thisfor a android app it shows me a warning that am doing too much work on the main thread.
Let me know if anyone knows a better way to do that!

Know when app data from Application Manager is deleted

I need to have an event that is called at the moment when the user of the phone has clicked 'Clear Data' in the 'Application Manager'. Is there a way to do that?
I need to know that because I got a database that holds very important information and I need to know when that information is deleted. I tried to look for database events for that and even events for data deletion with no luck.
There is no such event.
The first opportunity to detect this is when your app is started next.
(If you're using SQLiteOpenHelper, your onCreate method will get called.)
However, there is no guarantee that the user will actually use your app again.

Categories