Printing Nested Values from Firebase Query with Swift












1















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?










share|improve this question





























    1















    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?










    share|improve this question



























      1












      1








      1








      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?










      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 26 '18 at 2:53









      Frank van Puffelen

      242k29387414




      242k29387414










      asked Nov 26 '18 at 0:13









      javarookiejavarookie

      185




      185
























          1 Answer
          1






          active

          oldest

          votes


















          1














          If this is your structure:



          enter image description here



            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:



          enter image description here



          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.






          share|improve this answer


























          • 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











          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
          });


          }
          });














          draft saved

          draft discarded


















          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









          1














          If this is your structure:



          enter image description here



            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:



          enter image description here



          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.






          share|improve this answer


























          • 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
















          1














          If this is your structure:



          enter image description here



            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:



          enter image description here



          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.






          share|improve this answer


























          • 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














          1












          1








          1







          If this is your structure:



          enter image description here



            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:



          enter image description here



          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.






          share|improve this answer















          If this is your structure:



          enter image description here



            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:



          enter image description here



          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.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          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



















          • 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




















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Tonle Sap (See)

          I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

          Guatemaltekische Davis-Cup-Mannschaft