Swift firebase query nested child nodes
I have the following set up in firebase:
I then query the firebase:
func getUserInfo(){
var tempUsers = [user]()
// query for the data
let itemRef = Database.database().reference(fromURL: "[url]").child("users")
itemRef.observe(.value, with: { snapshot in
for child in snapshot.children {
if let childSnapshot = child as? DataSnapshot,
let dict = childSnapshot.value as? [String: Any],
let sport = dict["Sport"] as? String,
let username = dict["username"] as? String{
let user = user(sport: sport, username: username)
tempUsers.append(user)
}
}
self.usersArray = tempUsers
}){ (error) in
print("the error is: (error.localizedDescription)")
}
}
Users:
class user {
var username: String
var sport: String
var teamInfo: [teamInfo]
init(username: String, sport: String, teamInfo: [teamInfo])
{
self.username = username
self.sport = sport
self.teamInfo = [teamInfo]
}
}
struct teamInfo {
var AwayTeam: String
var HomeTeam = String
var Jersey = Int
}
I would like to know how would I be able to get the Team information within my query and then be able to add it to the instance of User, I have tried to make "Team" into a dict but this does not give me the intended result.
swift firebase firebase-realtime-database
add a comment |
I have the following set up in firebase:
I then query the firebase:
func getUserInfo(){
var tempUsers = [user]()
// query for the data
let itemRef = Database.database().reference(fromURL: "[url]").child("users")
itemRef.observe(.value, with: { snapshot in
for child in snapshot.children {
if let childSnapshot = child as? DataSnapshot,
let dict = childSnapshot.value as? [String: Any],
let sport = dict["Sport"] as? String,
let username = dict["username"] as? String{
let user = user(sport: sport, username: username)
tempUsers.append(user)
}
}
self.usersArray = tempUsers
}){ (error) in
print("the error is: (error.localizedDescription)")
}
}
Users:
class user {
var username: String
var sport: String
var teamInfo: [teamInfo]
init(username: String, sport: String, teamInfo: [teamInfo])
{
self.username = username
self.sport = sport
self.teamInfo = [teamInfo]
}
}
struct teamInfo {
var AwayTeam: String
var HomeTeam = String
var Jersey = Int
}
I would like to know how would I be able to get the Team information within my query and then be able to add it to the instance of User, I have tried to make "Team" into a dict but this does not give me the intended result.
swift firebase firebase-realtime-database
add a comment |
I have the following set up in firebase:
I then query the firebase:
func getUserInfo(){
var tempUsers = [user]()
// query for the data
let itemRef = Database.database().reference(fromURL: "[url]").child("users")
itemRef.observe(.value, with: { snapshot in
for child in snapshot.children {
if let childSnapshot = child as? DataSnapshot,
let dict = childSnapshot.value as? [String: Any],
let sport = dict["Sport"] as? String,
let username = dict["username"] as? String{
let user = user(sport: sport, username: username)
tempUsers.append(user)
}
}
self.usersArray = tempUsers
}){ (error) in
print("the error is: (error.localizedDescription)")
}
}
Users:
class user {
var username: String
var sport: String
var teamInfo: [teamInfo]
init(username: String, sport: String, teamInfo: [teamInfo])
{
self.username = username
self.sport = sport
self.teamInfo = [teamInfo]
}
}
struct teamInfo {
var AwayTeam: String
var HomeTeam = String
var Jersey = Int
}
I would like to know how would I be able to get the Team information within my query and then be able to add it to the instance of User, I have tried to make "Team" into a dict but this does not give me the intended result.
swift firebase firebase-realtime-database
I have the following set up in firebase:
I then query the firebase:
func getUserInfo(){
var tempUsers = [user]()
// query for the data
let itemRef = Database.database().reference(fromURL: "[url]").child("users")
itemRef.observe(.value, with: { snapshot in
for child in snapshot.children {
if let childSnapshot = child as? DataSnapshot,
let dict = childSnapshot.value as? [String: Any],
let sport = dict["Sport"] as? String,
let username = dict["username"] as? String{
let user = user(sport: sport, username: username)
tempUsers.append(user)
}
}
self.usersArray = tempUsers
}){ (error) in
print("the error is: (error.localizedDescription)")
}
}
Users:
class user {
var username: String
var sport: String
var teamInfo: [teamInfo]
init(username: String, sport: String, teamInfo: [teamInfo])
{
self.username = username
self.sport = sport
self.teamInfo = [teamInfo]
}
}
struct teamInfo {
var AwayTeam: String
var HomeTeam = String
var Jersey = Int
}
I would like to know how would I be able to get the Team information within my query and then be able to add it to the instance of User, I have tried to make "Team" into a dict but this does not give me the intended result.
swift firebase firebase-realtime-database
swift firebase firebase-realtime-database
edited Jan 2 at 19:15
Frank van Puffelen
244k29387415
244k29387415
asked Jan 2 at 19:12
AceAce
2381620
2381620
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
First things first: you have an inconsistency in username
. Your JSON spells it Username
with an uppercase U
, while the code uses username
with a lowercase u
. Since Firebase is case-sensitive, your code will right now never get the correct value.
Aside from that, you can get the values from the nested children by using DataSnapshot.childSnapshot(forPath:)
.
itemRef.observe(.value, with: { snapshot in
for child in snapshot.children {
if let childSnapshot = child as? DataSnapshot,
let dict = childSnapshot.value as? [String: Any],
let team = childSnapshot.childSnapshot(forPath: "Team/AwayTeam").value as? String;
would there be a way without including "Team/AwayTeam" as it is possible not every use has an AwayTeam there for it might not be in the data
– Ace
Jan 2 at 19:24
I'm not sure what you mean there, but mostly includedTeam/AwayTeam
as an example. If it doesn't exist,team
will benil
. And you can read the other properties in a similar way, e.g.Team/HomeTeam
.
– Frank van Puffelen
Jan 2 at 19:39
oh ok thank you for the clarification
– Ace
Jan 2 at 19:42
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%2f54011910%2fswift-firebase-query-nested-child-nodes%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
First things first: you have an inconsistency in username
. Your JSON spells it Username
with an uppercase U
, while the code uses username
with a lowercase u
. Since Firebase is case-sensitive, your code will right now never get the correct value.
Aside from that, you can get the values from the nested children by using DataSnapshot.childSnapshot(forPath:)
.
itemRef.observe(.value, with: { snapshot in
for child in snapshot.children {
if let childSnapshot = child as? DataSnapshot,
let dict = childSnapshot.value as? [String: Any],
let team = childSnapshot.childSnapshot(forPath: "Team/AwayTeam").value as? String;
would there be a way without including "Team/AwayTeam" as it is possible not every use has an AwayTeam there for it might not be in the data
– Ace
Jan 2 at 19:24
I'm not sure what you mean there, but mostly includedTeam/AwayTeam
as an example. If it doesn't exist,team
will benil
. And you can read the other properties in a similar way, e.g.Team/HomeTeam
.
– Frank van Puffelen
Jan 2 at 19:39
oh ok thank you for the clarification
– Ace
Jan 2 at 19:42
add a comment |
First things first: you have an inconsistency in username
. Your JSON spells it Username
with an uppercase U
, while the code uses username
with a lowercase u
. Since Firebase is case-sensitive, your code will right now never get the correct value.
Aside from that, you can get the values from the nested children by using DataSnapshot.childSnapshot(forPath:)
.
itemRef.observe(.value, with: { snapshot in
for child in snapshot.children {
if let childSnapshot = child as? DataSnapshot,
let dict = childSnapshot.value as? [String: Any],
let team = childSnapshot.childSnapshot(forPath: "Team/AwayTeam").value as? String;
would there be a way without including "Team/AwayTeam" as it is possible not every use has an AwayTeam there for it might not be in the data
– Ace
Jan 2 at 19:24
I'm not sure what you mean there, but mostly includedTeam/AwayTeam
as an example. If it doesn't exist,team
will benil
. And you can read the other properties in a similar way, e.g.Team/HomeTeam
.
– Frank van Puffelen
Jan 2 at 19:39
oh ok thank you for the clarification
– Ace
Jan 2 at 19:42
add a comment |
First things first: you have an inconsistency in username
. Your JSON spells it Username
with an uppercase U
, while the code uses username
with a lowercase u
. Since Firebase is case-sensitive, your code will right now never get the correct value.
Aside from that, you can get the values from the nested children by using DataSnapshot.childSnapshot(forPath:)
.
itemRef.observe(.value, with: { snapshot in
for child in snapshot.children {
if let childSnapshot = child as? DataSnapshot,
let dict = childSnapshot.value as? [String: Any],
let team = childSnapshot.childSnapshot(forPath: "Team/AwayTeam").value as? String;
First things first: you have an inconsistency in username
. Your JSON spells it Username
with an uppercase U
, while the code uses username
with a lowercase u
. Since Firebase is case-sensitive, your code will right now never get the correct value.
Aside from that, you can get the values from the nested children by using DataSnapshot.childSnapshot(forPath:)
.
itemRef.observe(.value, with: { snapshot in
for child in snapshot.children {
if let childSnapshot = child as? DataSnapshot,
let dict = childSnapshot.value as? [String: Any],
let team = childSnapshot.childSnapshot(forPath: "Team/AwayTeam").value as? String;
answered Jan 2 at 19:18
Frank van PuffelenFrank van Puffelen
244k29387415
244k29387415
would there be a way without including "Team/AwayTeam" as it is possible not every use has an AwayTeam there for it might not be in the data
– Ace
Jan 2 at 19:24
I'm not sure what you mean there, but mostly includedTeam/AwayTeam
as an example. If it doesn't exist,team
will benil
. And you can read the other properties in a similar way, e.g.Team/HomeTeam
.
– Frank van Puffelen
Jan 2 at 19:39
oh ok thank you for the clarification
– Ace
Jan 2 at 19:42
add a comment |
would there be a way without including "Team/AwayTeam" as it is possible not every use has an AwayTeam there for it might not be in the data
– Ace
Jan 2 at 19:24
I'm not sure what you mean there, but mostly includedTeam/AwayTeam
as an example. If it doesn't exist,team
will benil
. And you can read the other properties in a similar way, e.g.Team/HomeTeam
.
– Frank van Puffelen
Jan 2 at 19:39
oh ok thank you for the clarification
– Ace
Jan 2 at 19:42
would there be a way without including "Team/AwayTeam" as it is possible not every use has an AwayTeam there for it might not be in the data
– Ace
Jan 2 at 19:24
would there be a way without including "Team/AwayTeam" as it is possible not every use has an AwayTeam there for it might not be in the data
– Ace
Jan 2 at 19:24
I'm not sure what you mean there, but mostly included
Team/AwayTeam
as an example. If it doesn't exist, team
will be nil
. And you can read the other properties in a similar way, e.g. Team/HomeTeam
.– Frank van Puffelen
Jan 2 at 19:39
I'm not sure what you mean there, but mostly included
Team/AwayTeam
as an example. If it doesn't exist, team
will be nil
. And you can read the other properties in a similar way, e.g. Team/HomeTeam
.– Frank van Puffelen
Jan 2 at 19:39
oh ok thank you for the clarification
– Ace
Jan 2 at 19:42
oh ok thank you for the clarification
– Ace
Jan 2 at 19:42
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%2f54011910%2fswift-firebase-query-nested-child-nodes%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