Should I query everytime I need something from Realm/SQlite? - android

I am using Realm/Sqllite for storing my user_id along with user names, and in my friends section I am showing all of my friends name along with other information where unique key is this user_id.
And in my friends section I am using recyclerview to show all of my friends data,
Now I was just wondering suppose I have 100-200 friends list now to show user name what I can do is query every time for user_id from Realm/Sqlite in order to show the name of the user. OR I can just query all user_id list parallely and store them in a HashMap and now for each user_id I can directly check in the hahsmap for getting user name.
I know second way will be faster but memory consuming also, and I checked the time taken by Realm/Sqlite for every query and it is around 1-2ms that's why I was thinking as it is not that slow I can directly query from Realm/Sqlite.
Do anyone of you use the other technique or just query every time you need to get something from database?

Related

How to Retrieve list of attributes from multiple Firebase nodes

My firebase DB looks like
Now I want to query the database such that I take all the users whose email id is in a query list.
For example in the above shown database structure, if I want to make a query such that find the users where username in ["allen20252482", "arne19712450"] which can give me two users.
Is it possible?
Currently I can query like https://xyz.firebaseio.com/users.json to get the complete user node data , But is it possible to filter like where I take only those users whose username is in the given query list?
I am using REST API endpoint to query the Firebase DB.
Any suggestion is very much appreciated
I suggest reading the documentation on filtering data. It sounds like you want to filter by a child key:
https://xyz.firebaseio.com/users.json?orderBy="username"&equalTo="abcdef"
You will only be able to specify one username at a time.

Is there a way in Firebase to automatically swap a list of id's into the object

I have a Firebase database with a list of users and a list of best friends,
a user's best friends can be pulled by using it's userid:
users list:
userid
user data
userid
user data
..
best friends list:
userid
userid of friend
userid of friend
...
userid
userid of friend
userid of friend
...
...
...
Is there a way using the Firebase api to get the friend's data in a list instead of a list of userid's of your friends when making the call to get your best friends ? Now i have to do 2 steps (get best friends id's -> loop users for friend's data)
No, there is not. There is no way to achieve this. You need to query your data twice, first time to get best friends id's and second to get the actual data of those friends. You cannot get that list in a single step, using a single query..
What I think from your description is that you want to perform a join.
There's no official "join" method in the Firebase Database SDK, but
you can use multiple listeners to combine data from multiple paths.
https://www.youtube.com/watch?v=Idu9EJPSxiY
check out this youtube video it will help

How to manage friends in Firebase chat app - Android

I am developing a Firebase chat app.
I have implemented Firebase email/password authentication. So, when user signups successfully, I have a code through which on every signup/register the app stores user details like name,email, etc in Firebase database under uid node.
Then, the user searches for a friend using a email id,
if the uid with that email id is present in the firebase database, the function returns true,
then, the app, adds that email id under
<FirebaseApp>
<users>
....users details...
<friends>
<userid>
<unique pushed id> : searched email // here the friend's email id is added
After adding the new friend,
I am using a recyclerview to show the friend list.
So, the app retrieves all the nodes under friends>> current user's uid>>
As my database structure describes I have only saved friends uid in friends database.
At first step the user will have only friends' email ids. Let say in List friendsList.
Then I use this friendsList to retrieve all the other details of friends like profile picture, name, contact and etc... which is saved under users node.
But using for each loop retrieving the other details of every friend, and then store in a array list, then paasing that arrayList to adapter and then displaying to recyclerview. is a very time consuming process.
Becuase, it can have other functions too. like converting Base64 image to bitmap etc.
So every time activity starts , following all above steps, will make a lengthy process.
other that this, I have another way, where I can save friends other's details too under friends node. like name, contact etc. at a point when a new friend added to the database.
But again another question arises.
yes, I can store friend's other details too... But what if the friend updates his profile picture, contact?
So, this procedure is not suitable in my case.
How can I create a whatsapp like friends list in my Firebase app, where I every user's friends list will be always updated with his/her friends details.
also, the will not go under any lengthy process.
The best approach is to save all the necessary details in the friends node as well.
You can just save the Opposite_Friends tag in each user, where just add all the uids in whose friends list user is present, So that whenever user updates any detail, you can update it in every users Friends node as well.

Parse relational data queries

I'm new to Parse.com and was having trouble designing the structure of my database, and how to retrieve the desired data.
In the database, each user (primary identifier as email) has a list of friends and a status boolean. The friend list contains the email of other users in the database. I need to get the status boolean for each of the friends in a particular users list, and preferably in a single query to the server.
What would be a good way to design our structure and retrieve this data. Currently, I made two data classes (tables), one containing each user with their boolean status, and another containing each user and their list of friends. Firstly I was not sure if this structure is the correct way to go. Secondly, I don't know how to retrieve the status boolean for each user in a single users friend list.
Edit I actually discovered the relation column type just yesterday, but I was unable to figure out how to use it. 1) How do I link a Persona to a User in code? I understand I need to use ObjectID here, but how?
2) How do I add other Personae (friends) to a relation of a single Persona (the user). I was unable to populate this relation column. I understand query can be used on the relation column, but I couldn't reach that far ahead without populating the relation column.
3) In my query to the server, am I pulling the entire table? Lets say a user has 2 friends. Is there a way for me to fetch only the current user, and the two friends, or am I pulling the entire table, and then doing my filtering on it. I am concerned with the network being burdened if my table of users grows big.
Edit Well I couldn't figure out relational queries perfectly just yet, however, I found a good solution to my problem. Since the list of friends changes very rarely, I'll be maintaining this list offline, resulting in a single query to the server of pulling in the status of my friends. Along with this list, I may or may not also decide to pull in my own data and get an updated friend list. Thank you for your help though.
The way to model many-to-many relations in parse is with the relation column type. This is the best choice to describe how a user has many friends who are users. If this is a social-network-like app, another good bit of advice is to create a class -- distinct from the parse User -- that describes users' public personae.
This is so you can have the parse User class remain as the private, customer relationship between your app and a real person (there are built in security constraints here). This other table, say we call it Persona, can have a pointer-typed column to its user, keep such things as nickname, profile image, etc. and also keep your boolean status.
_User class - default stuff that comes standard with parse, plus anything pertaining to the customer relationship with your app.
Persona - pointer to _User table, boolean status, other public info, relation called "friends" relating this to other Persona.
So, given a logged in user and his/her currently selected persona (your choice whether users may have more than one personae), you can get friends' personae as follows (in pseudo code):
friendsRelation <- myPersona.friends
friendsQuery <- friendsRelation.query // query is a method on relation
run friendsQuery asynch, then the result will be allFriendsPersonae
for each persona in allFriendsPersonae
status <- persona.status
If you choose not to take the persona class advice, the "code" above is the same, just replace persona with user.
Edit - in response to question edit:
1) Link a persona the user by setting the persona's user column (pointer type) to the user object. To get that persona later, when you only have a user, query the persona table where "user" column equals user.
2) Relation implements an add() method. If you have a personaA, and want to add personaB as a friend, you getRelation("friends") on personaA, and send it add(personaB).
3) The query you get from a relation is a query only for members of that relation. So if personaA has two friends personaB and personaC, you'll get only B and C when you run personaA's friends query.

Database to use for storing profile information

I'm making an application for android which is used to store profile information of a user( FirstName, LastName, Email, Username, Password ). It will also store profile picture of the person. You can think of applications like WhatsApp, Viber Tango. There are many more like it.
In order to store and access this information easily and efficiently, how should these information be stored? I'm thinking about storing this information in Relational Database like MySql. I will have FN,LN,Email,Username, password and link to profile picture as columns of table. One more issue I had in mind is that if a user has say 100 friends, should I create a new table per user so that all his/her friends can be separate and accessed from single table or I should run some complex SQL query so that it returns list of friends of user?
Thanks a lot
I believe you are on the right track. MySQL is a fine choice for storing the data as long as it is on a server that can be accessed by your application.
You will need 2 tables: one to store all of the users and another to map the users friends.
Your users table will be pretty straight forward as you have already laid out. The other table (could be named something like user_friends) would have a column containing the id of the friend that made the request, another column containing the id of the friend that accepted the request, and any other x amount of columns you may need to contain information about the friendship. Any record without an id for the accepted friend can be displayed as a pending request.
I would make sure that there can only be one record containing a mapping of friends to prevent duplicates as well.

Categories

Resources