Realtime database using firebase - retrieving a specific information
So I am trying to create a real-time multiplayer quiz. I am using the Google Real-time Multiplayer API together with the firebase real-time database.
At the beginning of the game, users are prompted to ask themselves a question and provide two answers, where 1 answer is correct. Then this information is sent do Firebase Real-time Database under the child "Games" -- "mRoomId" --- "userId"
The variable uniqueRoomID is the specific id of the room which they players are retrieving from the Google API.
DatabaseReference myRef = database.getReference("Games").child(mRoomId).child(userId);
And I send 4 values with the setValue function to that specific reference.
Upload upload = new Upload(question,answer1,answer2, userId);
myRef.setValue(upload);
My database tree is as follows
My question is:
When I present a question to the other user later in the game, I don't want to show my own question, only the other user's question is relevant.
This is my valueEventListener:
myReference = FirebaseDatabase.getInstance().getReference().child("Games").child(mRoomId);
myReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
showData(dataSnapshot);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
And this is the method showData();
private void showData (DataSnapshot dataSnapshot){
for(DataSnapshot ds : dataSnapshot.getChildren()){
Upload upload = ds.getValue(Upload.class);
Log.d("TAG", "question1 is " + upload.getQuestion1() );
}
}
The log in showData is returning two different questions from the database because it loops through the children.
How do I get only one of the questions that is relevant to be presented to the user playing the game ?
Should I structure this differently, or is there a way to get only the values that are relevant to me?
java android firebase firebase-realtime-database
add a comment |
So I am trying to create a real-time multiplayer quiz. I am using the Google Real-time Multiplayer API together with the firebase real-time database.
At the beginning of the game, users are prompted to ask themselves a question and provide two answers, where 1 answer is correct. Then this information is sent do Firebase Real-time Database under the child "Games" -- "mRoomId" --- "userId"
The variable uniqueRoomID is the specific id of the room which they players are retrieving from the Google API.
DatabaseReference myRef = database.getReference("Games").child(mRoomId).child(userId);
And I send 4 values with the setValue function to that specific reference.
Upload upload = new Upload(question,answer1,answer2, userId);
myRef.setValue(upload);
My database tree is as follows
My question is:
When I present a question to the other user later in the game, I don't want to show my own question, only the other user's question is relevant.
This is my valueEventListener:
myReference = FirebaseDatabase.getInstance().getReference().child("Games").child(mRoomId);
myReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
showData(dataSnapshot);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
And this is the method showData();
private void showData (DataSnapshot dataSnapshot){
for(DataSnapshot ds : dataSnapshot.getChildren()){
Upload upload = ds.getValue(Upload.class);
Log.d("TAG", "question1 is " + upload.getQuestion1() );
}
}
The log in showData is returning two different questions from the database because it loops through the children.
How do I get only one of the questions that is relevant to be presented to the user playing the game ?
Should I structure this differently, or is there a way to get only the values that are relevant to me?
java android firebase firebase-realtime-database
1
Please add the code that you using now.
– Alex Mamo
Nov 19 '18 at 11:28
Which quiz question are you talking about (I can't see any node in your database named like that), how do you present a user the question. Please clear things out, the question is too ambiguous at the moment.
– PradyumanDixit
Nov 19 '18 at 14:52
I have updated my question with relevant code
– Guy Dude
Nov 20 '18 at 16:55
add a comment |
So I am trying to create a real-time multiplayer quiz. I am using the Google Real-time Multiplayer API together with the firebase real-time database.
At the beginning of the game, users are prompted to ask themselves a question and provide two answers, where 1 answer is correct. Then this information is sent do Firebase Real-time Database under the child "Games" -- "mRoomId" --- "userId"
The variable uniqueRoomID is the specific id of the room which they players are retrieving from the Google API.
DatabaseReference myRef = database.getReference("Games").child(mRoomId).child(userId);
And I send 4 values with the setValue function to that specific reference.
Upload upload = new Upload(question,answer1,answer2, userId);
myRef.setValue(upload);
My database tree is as follows
My question is:
When I present a question to the other user later in the game, I don't want to show my own question, only the other user's question is relevant.
This is my valueEventListener:
myReference = FirebaseDatabase.getInstance().getReference().child("Games").child(mRoomId);
myReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
showData(dataSnapshot);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
And this is the method showData();
private void showData (DataSnapshot dataSnapshot){
for(DataSnapshot ds : dataSnapshot.getChildren()){
Upload upload = ds.getValue(Upload.class);
Log.d("TAG", "question1 is " + upload.getQuestion1() );
}
}
The log in showData is returning two different questions from the database because it loops through the children.
How do I get only one of the questions that is relevant to be presented to the user playing the game ?
Should I structure this differently, or is there a way to get only the values that are relevant to me?
java android firebase firebase-realtime-database
So I am trying to create a real-time multiplayer quiz. I am using the Google Real-time Multiplayer API together with the firebase real-time database.
At the beginning of the game, users are prompted to ask themselves a question and provide two answers, where 1 answer is correct. Then this information is sent do Firebase Real-time Database under the child "Games" -- "mRoomId" --- "userId"
The variable uniqueRoomID is the specific id of the room which they players are retrieving from the Google API.
DatabaseReference myRef = database.getReference("Games").child(mRoomId).child(userId);
And I send 4 values with the setValue function to that specific reference.
Upload upload = new Upload(question,answer1,answer2, userId);
myRef.setValue(upload);
My database tree is as follows
My question is:
When I present a question to the other user later in the game, I don't want to show my own question, only the other user's question is relevant.
This is my valueEventListener:
myReference = FirebaseDatabase.getInstance().getReference().child("Games").child(mRoomId);
myReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
showData(dataSnapshot);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
And this is the method showData();
private void showData (DataSnapshot dataSnapshot){
for(DataSnapshot ds : dataSnapshot.getChildren()){
Upload upload = ds.getValue(Upload.class);
Log.d("TAG", "question1 is " + upload.getQuestion1() );
}
}
The log in showData is returning two different questions from the database because it loops through the children.
How do I get only one of the questions that is relevant to be presented to the user playing the game ?
Should I structure this differently, or is there a way to get only the values that are relevant to me?
java android firebase firebase-realtime-database
java android firebase firebase-realtime-database
edited Nov 20 '18 at 14:55
Guy Dude
asked Nov 19 '18 at 11:19
Guy DudeGuy Dude
61
61
1
Please add the code that you using now.
– Alex Mamo
Nov 19 '18 at 11:28
Which quiz question are you talking about (I can't see any node in your database named like that), how do you present a user the question. Please clear things out, the question is too ambiguous at the moment.
– PradyumanDixit
Nov 19 '18 at 14:52
I have updated my question with relevant code
– Guy Dude
Nov 20 '18 at 16:55
add a comment |
1
Please add the code that you using now.
– Alex Mamo
Nov 19 '18 at 11:28
Which quiz question are you talking about (I can't see any node in your database named like that), how do you present a user the question. Please clear things out, the question is too ambiguous at the moment.
– PradyumanDixit
Nov 19 '18 at 14:52
I have updated my question with relevant code
– Guy Dude
Nov 20 '18 at 16:55
1
1
Please add the code that you using now.
– Alex Mamo
Nov 19 '18 at 11:28
Please add the code that you using now.
– Alex Mamo
Nov 19 '18 at 11:28
Which quiz question are you talking about (I can't see any node in your database named like that), how do you present a user the question. Please clear things out, the question is too ambiguous at the moment.
– PradyumanDixit
Nov 19 '18 at 14:52
Which quiz question are you talking about (I can't see any node in your database named like that), how do you present a user the question. Please clear things out, the question is too ambiguous at the moment.
– PradyumanDixit
Nov 19 '18 at 14:52
I have updated my question with relevant code
– Guy Dude
Nov 20 '18 at 16:55
I have updated my question with relevant code
– Guy Dude
Nov 20 '18 at 16:55
add a comment |
1 Answer
1
active
oldest
votes
There is no way to exclude a specific child node from a query. This means that (as far as I can see) you have two basic options:
- Read both child nodes, and remove your own node client side based on the fact that its
DataSnapshot.getKey()
has the same value asUser.getUid()
. - Read only the child node of the other player. This requires that you somehow already know the key/UID of the other player, but would allow you to then load only that player's data with
DatabaseReference theirRef = database.getReference("Games").child(mRoomId).child(otherPlayerUid);
Also see:
- is it possible query data that are not equal to the specified condition?
- Firebase retrieving all children except one
- Firebase : How to read every child node in parent except children with particular data
1: In my case the getKey is actually returning the child "mRoomId" which is identical for user1 and user2. 2: I have tried to find a way to easily share this information between the two users (userId), but I have no luck at the moment. It will get more complex later in the development because it might include 3- or 4 users aswell..
– Guy Dude
Nov 20 '18 at 14:59
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53373519%2frealtime-database-using-firebase-retrieving-a-specific-information%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
There is no way to exclude a specific child node from a query. This means that (as far as I can see) you have two basic options:
- Read both child nodes, and remove your own node client side based on the fact that its
DataSnapshot.getKey()
has the same value asUser.getUid()
. - Read only the child node of the other player. This requires that you somehow already know the key/UID of the other player, but would allow you to then load only that player's data with
DatabaseReference theirRef = database.getReference("Games").child(mRoomId).child(otherPlayerUid);
Also see:
- is it possible query data that are not equal to the specified condition?
- Firebase retrieving all children except one
- Firebase : How to read every child node in parent except children with particular data
1: In my case the getKey is actually returning the child "mRoomId" which is identical for user1 and user2. 2: I have tried to find a way to easily share this information between the two users (userId), but I have no luck at the moment. It will get more complex later in the development because it might include 3- or 4 users aswell..
– Guy Dude
Nov 20 '18 at 14:59
add a comment |
There is no way to exclude a specific child node from a query. This means that (as far as I can see) you have two basic options:
- Read both child nodes, and remove your own node client side based on the fact that its
DataSnapshot.getKey()
has the same value asUser.getUid()
. - Read only the child node of the other player. This requires that you somehow already know the key/UID of the other player, but would allow you to then load only that player's data with
DatabaseReference theirRef = database.getReference("Games").child(mRoomId).child(otherPlayerUid);
Also see:
- is it possible query data that are not equal to the specified condition?
- Firebase retrieving all children except one
- Firebase : How to read every child node in parent except children with particular data
1: In my case the getKey is actually returning the child "mRoomId" which is identical for user1 and user2. 2: I have tried to find a way to easily share this information between the two users (userId), but I have no luck at the moment. It will get more complex later in the development because it might include 3- or 4 users aswell..
– Guy Dude
Nov 20 '18 at 14:59
add a comment |
There is no way to exclude a specific child node from a query. This means that (as far as I can see) you have two basic options:
- Read both child nodes, and remove your own node client side based on the fact that its
DataSnapshot.getKey()
has the same value asUser.getUid()
. - Read only the child node of the other player. This requires that you somehow already know the key/UID of the other player, but would allow you to then load only that player's data with
DatabaseReference theirRef = database.getReference("Games").child(mRoomId).child(otherPlayerUid);
Also see:
- is it possible query data that are not equal to the specified condition?
- Firebase retrieving all children except one
- Firebase : How to read every child node in parent except children with particular data
There is no way to exclude a specific child node from a query. This means that (as far as I can see) you have two basic options:
- Read both child nodes, and remove your own node client side based on the fact that its
DataSnapshot.getKey()
has the same value asUser.getUid()
. - Read only the child node of the other player. This requires that you somehow already know the key/UID of the other player, but would allow you to then load only that player's data with
DatabaseReference theirRef = database.getReference("Games").child(mRoomId).child(otherPlayerUid);
Also see:
- is it possible query data that are not equal to the specified condition?
- Firebase retrieving all children except one
- Firebase : How to read every child node in parent except children with particular data
answered Nov 19 '18 at 15:39
Frank van PuffelenFrank van Puffelen
233k29380406
233k29380406
1: In my case the getKey is actually returning the child "mRoomId" which is identical for user1 and user2. 2: I have tried to find a way to easily share this information between the two users (userId), but I have no luck at the moment. It will get more complex later in the development because it might include 3- or 4 users aswell..
– Guy Dude
Nov 20 '18 at 14:59
add a comment |
1: In my case the getKey is actually returning the child "mRoomId" which is identical for user1 and user2. 2: I have tried to find a way to easily share this information between the two users (userId), but I have no luck at the moment. It will get more complex later in the development because it might include 3- or 4 users aswell..
– Guy Dude
Nov 20 '18 at 14:59
1: In my case the getKey is actually returning the child "mRoomId" which is identical for user1 and user2. 2: I have tried to find a way to easily share this information between the two users (userId), but I have no luck at the moment. It will get more complex later in the development because it might include 3- or 4 users aswell..
– Guy Dude
Nov 20 '18 at 14:59
1: In my case the getKey is actually returning the child "mRoomId" which is identical for user1 and user2. 2: I have tried to find a way to easily share this information between the two users (userId), but I have no luck at the moment. It will get more complex later in the development because it might include 3- or 4 users aswell..
– Guy Dude
Nov 20 '18 at 14:59
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53373519%2frealtime-database-using-firebase-retrieving-a-specific-information%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Please add the code that you using now.
– Alex Mamo
Nov 19 '18 at 11:28
Which quiz question are you talking about (I can't see any node in your database named like that), how do you present a user the question. Please clear things out, the question is too ambiguous at the moment.
– PradyumanDixit
Nov 19 '18 at 14:52
I have updated my question with relevant code
– Guy Dude
Nov 20 '18 at 16:55