Printing Nested Values from Firebase Query with Swift
I currently have a firebase database structured like so:
Playlist
Playlist1
0: "song1"
1: "song2"
Playlist2
0: "song1"
1: "song2"
What I would like to do print the name of each playlist name onto a TableViewController. I understand how the TableView works, but what I'm hung up on is how to make the call to the database. I have hardcoded for now as
let ref = Database.database().reference().child("Playlists/Playlist1")
ref.observe(.value, with: {
snapshot in
print(snapshot.key)
This obviously isn't scalable, is there anyway I can iterate over all the data and just get the playlist names?
swift firebase firebase-realtime-database
add a comment |
I currently have a firebase database structured like so:
Playlist
Playlist1
0: "song1"
1: "song2"
Playlist2
0: "song1"
1: "song2"
What I would like to do print the name of each playlist name onto a TableViewController. I understand how the TableView works, but what I'm hung up on is how to make the call to the database. I have hardcoded for now as
let ref = Database.database().reference().child("Playlists/Playlist1")
ref.observe(.value, with: {
snapshot in
print(snapshot.key)
This obviously isn't scalable, is there anyway I can iterate over all the data and just get the playlist names?
swift firebase firebase-realtime-database
add a comment |
I currently have a firebase database structured like so:
Playlist
Playlist1
0: "song1"
1: "song2"
Playlist2
0: "song1"
1: "song2"
What I would like to do print the name of each playlist name onto a TableViewController. I understand how the TableView works, but what I'm hung up on is how to make the call to the database. I have hardcoded for now as
let ref = Database.database().reference().child("Playlists/Playlist1")
ref.observe(.value, with: {
snapshot in
print(snapshot.key)
This obviously isn't scalable, is there anyway I can iterate over all the data and just get the playlist names?
swift firebase firebase-realtime-database
I currently have a firebase database structured like so:
Playlist
Playlist1
0: "song1"
1: "song2"
Playlist2
0: "song1"
1: "song2"
What I would like to do print the name of each playlist name onto a TableViewController. I understand how the TableView works, but what I'm hung up on is how to make the call to the database. I have hardcoded for now as
let ref = Database.database().reference().child("Playlists/Playlist1")
ref.observe(.value, with: {
snapshot in
print(snapshot.key)
This obviously isn't scalable, is there anyway I can iterate over all the data and just get the playlist names?
swift firebase firebase-realtime-database
swift firebase firebase-realtime-database
edited Nov 26 '18 at 2:53
Frank van Puffelen
242k29387414
242k29387414
asked Nov 26 '18 at 0:13
javarookiejavarookie
185
185
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
If this is your structure:

Database.database().reference().child("Playlists").observe(.value, with: { (_snapshot) in
if let playLists = _snapshot.value as? [String : Any] {
var names = [String]()
for playlist in playLists {
names.append(playlist.key)
}
// return names in a completion
}
}, withCancel: nil)
outputs:

I would also recommend to NOT use the name of the playlist as a key, you should use an ID like childByAutoID, it's a built in firebase method. Then, have the name as attribute.
I added a print statement inside the for loop and there was no data outputted. To your point, do you think it would be best to structure the data like Playlists: 0: 0: "song1" 1: "song2" name: "Playlist1"
– javarookie
Nov 26 '18 at 0:37
1
check new edit, I think your parent node is called 'Playlist' instead of 'Playlists'
– Gustavo Vollbrecht
Nov 26 '18 at 0:38
1
not sequential, use childByAutoID when inserting new values.
– Gustavo Vollbrecht
Nov 26 '18 at 0:41
okay, changed the parent node to 'Playlists' (kept the original structure), still no data being outputted
– javarookie
Nov 26 '18 at 0:46
Thanks for the help, works great now! I think I made it hard on myself with how I structured the database initially, so I'll follow your advice
– javarookie
Nov 26 '18 at 1:01
|
show 1 more 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%2f53473301%2fprinting-nested-values-from-firebase-query-with-swift%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
If this is your structure:

Database.database().reference().child("Playlists").observe(.value, with: { (_snapshot) in
if let playLists = _snapshot.value as? [String : Any] {
var names = [String]()
for playlist in playLists {
names.append(playlist.key)
}
// return names in a completion
}
}, withCancel: nil)
outputs:

I would also recommend to NOT use the name of the playlist as a key, you should use an ID like childByAutoID, it's a built in firebase method. Then, have the name as attribute.
I added a print statement inside the for loop and there was no data outputted. To your point, do you think it would be best to structure the data like Playlists: 0: 0: "song1" 1: "song2" name: "Playlist1"
– javarookie
Nov 26 '18 at 0:37
1
check new edit, I think your parent node is called 'Playlist' instead of 'Playlists'
– Gustavo Vollbrecht
Nov 26 '18 at 0:38
1
not sequential, use childByAutoID when inserting new values.
– Gustavo Vollbrecht
Nov 26 '18 at 0:41
okay, changed the parent node to 'Playlists' (kept the original structure), still no data being outputted
– javarookie
Nov 26 '18 at 0:46
Thanks for the help, works great now! I think I made it hard on myself with how I structured the database initially, so I'll follow your advice
– javarookie
Nov 26 '18 at 1:01
|
show 1 more comment
If this is your structure:

Database.database().reference().child("Playlists").observe(.value, with: { (_snapshot) in
if let playLists = _snapshot.value as? [String : Any] {
var names = [String]()
for playlist in playLists {
names.append(playlist.key)
}
// return names in a completion
}
}, withCancel: nil)
outputs:

I would also recommend to NOT use the name of the playlist as a key, you should use an ID like childByAutoID, it's a built in firebase method. Then, have the name as attribute.
I added a print statement inside the for loop and there was no data outputted. To your point, do you think it would be best to structure the data like Playlists: 0: 0: "song1" 1: "song2" name: "Playlist1"
– javarookie
Nov 26 '18 at 0:37
1
check new edit, I think your parent node is called 'Playlist' instead of 'Playlists'
– Gustavo Vollbrecht
Nov 26 '18 at 0:38
1
not sequential, use childByAutoID when inserting new values.
– Gustavo Vollbrecht
Nov 26 '18 at 0:41
okay, changed the parent node to 'Playlists' (kept the original structure), still no data being outputted
– javarookie
Nov 26 '18 at 0:46
Thanks for the help, works great now! I think I made it hard on myself with how I structured the database initially, so I'll follow your advice
– javarookie
Nov 26 '18 at 1:01
|
show 1 more comment
If this is your structure:

Database.database().reference().child("Playlists").observe(.value, with: { (_snapshot) in
if let playLists = _snapshot.value as? [String : Any] {
var names = [String]()
for playlist in playLists {
names.append(playlist.key)
}
// return names in a completion
}
}, withCancel: nil)
outputs:

I would also recommend to NOT use the name of the playlist as a key, you should use an ID like childByAutoID, it's a built in firebase method. Then, have the name as attribute.
If this is your structure:

Database.database().reference().child("Playlists").observe(.value, with: { (_snapshot) in
if let playLists = _snapshot.value as? [String : Any] {
var names = [String]()
for playlist in playLists {
names.append(playlist.key)
}
// return names in a completion
}
}, withCancel: nil)
outputs:

I would also recommend to NOT use the name of the playlist as a key, you should use an ID like childByAutoID, it's a built in firebase method. Then, have the name as attribute.
edited Nov 26 '18 at 0:49
answered Nov 26 '18 at 0:19
Gustavo VollbrechtGustavo Vollbrecht
1,6581922
1,6581922
I added a print statement inside the for loop and there was no data outputted. To your point, do you think it would be best to structure the data like Playlists: 0: 0: "song1" 1: "song2" name: "Playlist1"
– javarookie
Nov 26 '18 at 0:37
1
check new edit, I think your parent node is called 'Playlist' instead of 'Playlists'
– Gustavo Vollbrecht
Nov 26 '18 at 0:38
1
not sequential, use childByAutoID when inserting new values.
– Gustavo Vollbrecht
Nov 26 '18 at 0:41
okay, changed the parent node to 'Playlists' (kept the original structure), still no data being outputted
– javarookie
Nov 26 '18 at 0:46
Thanks for the help, works great now! I think I made it hard on myself with how I structured the database initially, so I'll follow your advice
– javarookie
Nov 26 '18 at 1:01
|
show 1 more comment
I added a print statement inside the for loop and there was no data outputted. To your point, do you think it would be best to structure the data like Playlists: 0: 0: "song1" 1: "song2" name: "Playlist1"
– javarookie
Nov 26 '18 at 0:37
1
check new edit, I think your parent node is called 'Playlist' instead of 'Playlists'
– Gustavo Vollbrecht
Nov 26 '18 at 0:38
1
not sequential, use childByAutoID when inserting new values.
– Gustavo Vollbrecht
Nov 26 '18 at 0:41
okay, changed the parent node to 'Playlists' (kept the original structure), still no data being outputted
– javarookie
Nov 26 '18 at 0:46
Thanks for the help, works great now! I think I made it hard on myself with how I structured the database initially, so I'll follow your advice
– javarookie
Nov 26 '18 at 1:01
I added a print statement inside the for loop and there was no data outputted. To your point, do you think it would be best to structure the data like Playlists: 0: 0: "song1" 1: "song2" name: "Playlist1"
– javarookie
Nov 26 '18 at 0:37
I added a print statement inside the for loop and there was no data outputted. To your point, do you think it would be best to structure the data like Playlists: 0: 0: "song1" 1: "song2" name: "Playlist1"
– javarookie
Nov 26 '18 at 0:37
1
1
check new edit, I think your parent node is called 'Playlist' instead of 'Playlists'
– Gustavo Vollbrecht
Nov 26 '18 at 0:38
check new edit, I think your parent node is called 'Playlist' instead of 'Playlists'
– Gustavo Vollbrecht
Nov 26 '18 at 0:38
1
1
not sequential, use childByAutoID when inserting new values.
– Gustavo Vollbrecht
Nov 26 '18 at 0:41
not sequential, use childByAutoID when inserting new values.
– Gustavo Vollbrecht
Nov 26 '18 at 0:41
okay, changed the parent node to 'Playlists' (kept the original structure), still no data being outputted
– javarookie
Nov 26 '18 at 0:46
okay, changed the parent node to 'Playlists' (kept the original structure), still no data being outputted
– javarookie
Nov 26 '18 at 0:46
Thanks for the help, works great now! I think I made it hard on myself with how I structured the database initially, so I'll follow your advice
– javarookie
Nov 26 '18 at 1:01
Thanks for the help, works great now! I think I made it hard on myself with how I structured the database initially, so I'll follow your advice
– javarookie
Nov 26 '18 at 1:01
|
show 1 more 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%2f53473301%2fprinting-nested-values-from-firebase-query-with-swift%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