How to get Firebase data only key without values? - android

i have 3 tables [users, tracks, trackLikers]. when user like a track it added to trackLikers like this.
tracklikers
703486
AXweXLKIQiQYmwia5TbvNbSLk472: Alex
AXweXLKIQiQYmwia5TbvNbSLk472: John
704875
AXweXLKIQiQYmwia5TbvNbSLk472: Alex
Now i want to get how many tracks "Alex" likes. So i do it like this.
dbReference.child("tracklikers").orderByChild("AXweXLKIQiQYmwia5TbvNbSLk472").equalTo("Alex").
Ok fine i got the result of 2 tracks id [703486, 704875]. But the problem is i also got the John in 703486 result. So if i could have one million likes on this track i will have to retrive all likers :-(. So any one have solution how to solve this problem. Or only i can get the track id without all likers values ?
if there is no solution so unfortunately i have to switch to relational database :-(.

You should have another node such as user-tracks to keep tracks which are liked by user.
user-tracks
- AXweXLKIQiQYmwia5TbvNbSLk472 // userId
- 703486 // track which user liked

Related

Firestore search across custom objects array

I try to make an app on Android for services reviews, every review is save like an element in the document "user" like this-
Everytime the user save a report I have see if inside the reports the field "plate" is existing comparing with the new one, I try query using the examples in the page arrays in Firebase
In the code i tried get with "whereEqualTo" method search by "reports.plate" or with the method "whereArrayContains" with the same field (the filter uid exits but in the first image doesn't show) any idea how i can search the document when some of the custom elements fields is equal
maybe sorry in advance for the english i'm practicing
Thanks in advance

Firebase data flattening

I have a quick question about the best practices for data structure in a firebase database.
I want users of my app to be able to maintain a friends list. The firebase documentation recommends creating a schema (not sure if thats the proper word in this context) that is as flat as possible. Because of this I thought it would be a good idea to separate the friends section from the player section in the database like so:
{
"players":{
"player1id":{
"username":"john",...
},
"player2id": ...,
"player3id": ...
}
"friends": {
"player1id"{
"friends":{
"friend1Id":true,
"friend2Id":true
}
},
}
"player2id"{
"friends":{
"friend1Id":true,
"friend2Id":true
}
},
}
}
So my questions are as follows:
Is this a good design for my schema?
When pulling a friends list for one player, will the friends lists of EVERY player be pulled? and if so, can this be avoided?
Also, what would be the best way to then pull in additional information about the friends once the app has all of their IDs. e.g. getting all of their user names which will be stored as a string in their player profile.
Is this a good design for my schema?
You're already thinking in the right direction. However the "friends" node can be simplified to:
"friends": {
"player1id": {
"friend1Id":true,
"friend2Id":true
}
}
Remember that Firebase node names cannot use the character dot (.). So if your IDs are integer such as 1, 2, and 3 everything is OK, but if the IDs are username be careful (for example "super123" is OK but "super.duper" is not)
When pulling a friends list for one player, will the friends lists of EVERY player be pulled? and if so, can this be avoided?
No. If you pull /friends/1 it obviously won't pull /friends/2 etc.
Also, what would be the best way to then pull in additional information about the friends once the app has all of their IDs. e.g. getting all of their user names which will be stored as a string in their player profile.
Loop through the IDs and fetch the respective nodes from Firebase again. For example if user 1 has friends 2, 3, and 4, then using a for loop fetch /players/2, /players/3, and /players/4
Since firebase pull works asynchronously, you might need to use a counter or some other mechanism so that when the last data is pulled you can continue running the completion code.

Android/Firebase Trying to show nested objects retrieved from Firebase in adapter

I'm trying to show a list of songs on a recyclerview in android, nothing too fancy. something like this:
Song1
in Album a
-Song2
in Album a
-Song3
in Album b
So far so good; also, my firebase db looks something like this (in my app, a song cannot be on two albums, so bear with me)
songs:
-idSong1:
description:"Song1"
album: idAlbumA
-idSong2:
description:"Song2"
album: idAlbumA
-idSong3:
description:"Song3"
album: idAlbumB
albums:
-idAlbumA:
description:"Album a"
-idAlbumB:
description:"Album b"
Okay, enter Android adapters. I set a firebase valueListener to songs, retrieve all of them and pass them on to the adapter. The problem is, I still don't have the description of their albums.
Let me clarify, I do have the id's of the albums, but I'm forced to set a value listener of every item to find out the album description. I'm not familiar with firebase but it doesn't take much to know that that can't be right. I would be setting and removing listener a lot when the user scrolls through the song list.
There is a high chance I might be approaching this whole thing the wrong way. So any kind of advice is welcome.
Lastly, I want to congratulate the guys at firebase for such an amazing product!
Well, it turns out what I was trying to do was to simply do a JOIN between two nodes. and here's how it's done: https://www.firebase.com/blog/2013-10-01-queries-part-one.html

Number of Data in a row checking

I am building a monopoly game and from what I'm doing I'm almost done, but I want the game to end after 30dice rolls. But the way I want to do it is weird. I need a way to store data and check if the data is upto 30 or not, I mean check the amount of data is a row, I've been looking if sqlite or shared preferences would do, but can't get anything. Any idea would be welcomed and if you can help review my code too, I wouldn't mind. Thank You.
If you realy want to persist it in the Database, you want to have a key-value table current_game_progress with an item dice_rolls and update this each dice roll.
UPDATE current_game_progress SET content = content + 1 WHERE keyname = "dice_rolls"
But its a bit overhead. Maybe you want to store it local in a variable and transfer it on "save" action to the database.

Lime Survey Submit answer from Android app

In an Android app i have implement LimeSurvey,in which list of different Survey are displayed from lime survey admin panel.
I am able to display questions of a survey but not getting any structure to submit user answer's of survey to LimeSurvey database.
Any one can please help me how can i submit answer and in what structure lime survey accept response of survey.
As document suggest to add response in format -
sSessionKey (string) - The session key
iSurveyID (integer) - The survey id
aResponseData (array) - array/structure containing your response data
so my query is what the structure for aResponseData (array) , so it properly store in lime survey database.
Edit:
How to add answer of sub-questions.I am able to submit answer of simple question but how to send answer of subquestion and in Survey question type-L(List of radio with comment)how to submit comment.
Thanks in advance.
The only way I found to solve this was downloading the .sql database backup from the main taskbar and look for your table, which is *lime_survey_IDSURVEY*, and check for the columns which match with your questions, and use them for your mapping in the aResponseData. I'll leave you an example of that:
{"method": "add_response", "params": {"sSessionKey ": "ey2bv8v7zaw7uwj6fa9a8mmx9xqiikja", "iSurveyID ": 588636, "aResponseData ": {"588636X1X1":"Aja","588636X1X22":"Y" } }, "id": 1}
This will be the JSON that I send with my POST request, where 588636X1X1 is a column that match with a question, and "Aja" is the answer (in that case is a text-string), meanwhile, the other one, it's the same but it's a multiple choice question. As you can see, there is a pattern for the column names, 588636 it's my survey ID, X1 it's the group of the question, and X1 it's the ID for my question.
Hope it's useful.
You should be using the limesurvey remote control feature to call the webservices api for various tasks you want to perform: add_response in this case . Its documentation is here
--
amit

Categories

Resources