Uploading comments to firebase database with swift












0














I am trying to upload comments into posts so that my database structure looks like the one in the screenshot, my download function works to download the comments but I'm not sure how to create a function to upload them to the appropriate post. If I pass through the postID could I upload comments using that?



func uploadMessage(withContent content: String, withIcon icon: String, withColor color: String, withDate date: String, withuserName userName: String,sendComplete: @escaping (_ status: Bool) -> ()){
REF_FEEDMESSAGES.childByAutoId().updateChildValues(["content" : content, "color" : color, "icon" : icon, "date" : date, "userName" : userName])
sendComplete(true)
}


func getFeedMessages(handler: @escaping (_ feedMessages:[FeedMessages]) -> ()){
var feedMessagesArray = [FeedMessages]()
var commentArray = [messageComments]()


REF_FEEDMESSAGES.observeSingleEvent(of: .value) { (feedMessagesSnapshot) in

guard let feedMessagesSnapshot = feedMessagesSnapshot.children.allObjects as? [DataSnapshot] else {return}
for messages in feedMessagesSnapshot {

let content = messages.childSnapshot(forPath: "content").value as? String ?? "Joe Flacco is an elite QB"
let icon = messages.childSnapshot(forPath: "icon").value as? String ?? "none"
var color = messages.childSnapshot(forPath: "color").value as? String ?? "bop"
let date = messages.childSnapshot(forPath: "date").value as? String ?? "0"
let userName = messages.childSnapshot(forPath: "userName").value as? String ?? "Anonymous"

let comments = messages.childSnapshot(forPath: "comments").value as? [String: Any] ?? [:]


for comment in comments {

let theComment = comment.value as? [String: Any]

let theContent = theComment?["content"] as? String ?? ""
let theIcon = theComment?["icon"] as? String ?? ""
let theColor = theComment?["color"] as? String ?? ""
let theDate = theComment?["date"] as? String ?? ""
let theName = theComment?["userName"] as? String ?? ""

let aComment = messageComments(content: theContent, color: theColor, icon: theIcon, date: theDate, userName: theName)
commentArray.append(aComment)
}

let messages = FeedMessages(content: content, color: color, icon: icon, date: date, comments: commentArray, userName: userName)
feedMessagesArray.append(messages)
}

handler(feedMessagesArray)
}
}


database structure










share|improve this question





























    0














    I am trying to upload comments into posts so that my database structure looks like the one in the screenshot, my download function works to download the comments but I'm not sure how to create a function to upload them to the appropriate post. If I pass through the postID could I upload comments using that?



    func uploadMessage(withContent content: String, withIcon icon: String, withColor color: String, withDate date: String, withuserName userName: String,sendComplete: @escaping (_ status: Bool) -> ()){
    REF_FEEDMESSAGES.childByAutoId().updateChildValues(["content" : content, "color" : color, "icon" : icon, "date" : date, "userName" : userName])
    sendComplete(true)
    }


    func getFeedMessages(handler: @escaping (_ feedMessages:[FeedMessages]) -> ()){
    var feedMessagesArray = [FeedMessages]()
    var commentArray = [messageComments]()


    REF_FEEDMESSAGES.observeSingleEvent(of: .value) { (feedMessagesSnapshot) in

    guard let feedMessagesSnapshot = feedMessagesSnapshot.children.allObjects as? [DataSnapshot] else {return}
    for messages in feedMessagesSnapshot {

    let content = messages.childSnapshot(forPath: "content").value as? String ?? "Joe Flacco is an elite QB"
    let icon = messages.childSnapshot(forPath: "icon").value as? String ?? "none"
    var color = messages.childSnapshot(forPath: "color").value as? String ?? "bop"
    let date = messages.childSnapshot(forPath: "date").value as? String ?? "0"
    let userName = messages.childSnapshot(forPath: "userName").value as? String ?? "Anonymous"

    let comments = messages.childSnapshot(forPath: "comments").value as? [String: Any] ?? [:]


    for comment in comments {

    let theComment = comment.value as? [String: Any]

    let theContent = theComment?["content"] as? String ?? ""
    let theIcon = theComment?["icon"] as? String ?? ""
    let theColor = theComment?["color"] as? String ?? ""
    let theDate = theComment?["date"] as? String ?? ""
    let theName = theComment?["userName"] as? String ?? ""

    let aComment = messageComments(content: theContent, color: theColor, icon: theIcon, date: theDate, userName: theName)
    commentArray.append(aComment)
    }

    let messages = FeedMessages(content: content, color: color, icon: icon, date: date, comments: commentArray, userName: userName)
    feedMessagesArray.append(messages)
    }

    handler(feedMessagesArray)
    }
    }


    database structure










    share|improve this question



























      0












      0








      0







      I am trying to upload comments into posts so that my database structure looks like the one in the screenshot, my download function works to download the comments but I'm not sure how to create a function to upload them to the appropriate post. If I pass through the postID could I upload comments using that?



      func uploadMessage(withContent content: String, withIcon icon: String, withColor color: String, withDate date: String, withuserName userName: String,sendComplete: @escaping (_ status: Bool) -> ()){
      REF_FEEDMESSAGES.childByAutoId().updateChildValues(["content" : content, "color" : color, "icon" : icon, "date" : date, "userName" : userName])
      sendComplete(true)
      }


      func getFeedMessages(handler: @escaping (_ feedMessages:[FeedMessages]) -> ()){
      var feedMessagesArray = [FeedMessages]()
      var commentArray = [messageComments]()


      REF_FEEDMESSAGES.observeSingleEvent(of: .value) { (feedMessagesSnapshot) in

      guard let feedMessagesSnapshot = feedMessagesSnapshot.children.allObjects as? [DataSnapshot] else {return}
      for messages in feedMessagesSnapshot {

      let content = messages.childSnapshot(forPath: "content").value as? String ?? "Joe Flacco is an elite QB"
      let icon = messages.childSnapshot(forPath: "icon").value as? String ?? "none"
      var color = messages.childSnapshot(forPath: "color").value as? String ?? "bop"
      let date = messages.childSnapshot(forPath: "date").value as? String ?? "0"
      let userName = messages.childSnapshot(forPath: "userName").value as? String ?? "Anonymous"

      let comments = messages.childSnapshot(forPath: "comments").value as? [String: Any] ?? [:]


      for comment in comments {

      let theComment = comment.value as? [String: Any]

      let theContent = theComment?["content"] as? String ?? ""
      let theIcon = theComment?["icon"] as? String ?? ""
      let theColor = theComment?["color"] as? String ?? ""
      let theDate = theComment?["date"] as? String ?? ""
      let theName = theComment?["userName"] as? String ?? ""

      let aComment = messageComments(content: theContent, color: theColor, icon: theIcon, date: theDate, userName: theName)
      commentArray.append(aComment)
      }

      let messages = FeedMessages(content: content, color: color, icon: icon, date: date, comments: commentArray, userName: userName)
      feedMessagesArray.append(messages)
      }

      handler(feedMessagesArray)
      }
      }


      database structure










      share|improve this question















      I am trying to upload comments into posts so that my database structure looks like the one in the screenshot, my download function works to download the comments but I'm not sure how to create a function to upload them to the appropriate post. If I pass through the postID could I upload comments using that?



      func uploadMessage(withContent content: String, withIcon icon: String, withColor color: String, withDate date: String, withuserName userName: String,sendComplete: @escaping (_ status: Bool) -> ()){
      REF_FEEDMESSAGES.childByAutoId().updateChildValues(["content" : content, "color" : color, "icon" : icon, "date" : date, "userName" : userName])
      sendComplete(true)
      }


      func getFeedMessages(handler: @escaping (_ feedMessages:[FeedMessages]) -> ()){
      var feedMessagesArray = [FeedMessages]()
      var commentArray = [messageComments]()


      REF_FEEDMESSAGES.observeSingleEvent(of: .value) { (feedMessagesSnapshot) in

      guard let feedMessagesSnapshot = feedMessagesSnapshot.children.allObjects as? [DataSnapshot] else {return}
      for messages in feedMessagesSnapshot {

      let content = messages.childSnapshot(forPath: "content").value as? String ?? "Joe Flacco is an elite QB"
      let icon = messages.childSnapshot(forPath: "icon").value as? String ?? "none"
      var color = messages.childSnapshot(forPath: "color").value as? String ?? "bop"
      let date = messages.childSnapshot(forPath: "date").value as? String ?? "0"
      let userName = messages.childSnapshot(forPath: "userName").value as? String ?? "Anonymous"

      let comments = messages.childSnapshot(forPath: "comments").value as? [String: Any] ?? [:]


      for comment in comments {

      let theComment = comment.value as? [String: Any]

      let theContent = theComment?["content"] as? String ?? ""
      let theIcon = theComment?["icon"] as? String ?? ""
      let theColor = theComment?["color"] as? String ?? ""
      let theDate = theComment?["date"] as? String ?? ""
      let theName = theComment?["userName"] as? String ?? ""

      let aComment = messageComments(content: theContent, color: theColor, icon: theIcon, date: theDate, userName: theName)
      commentArray.append(aComment)
      }

      let messages = FeedMessages(content: content, color: color, icon: icon, date: date, comments: commentArray, userName: userName)
      feedMessagesArray.append(messages)
      }

      handler(feedMessagesArray)
      }
      }


      database structure







      ios swift firebase firebase-realtime-database






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 1:42









      KENdi

      5,7092821




      5,7092821










      asked Nov 23 '18 at 1:30









      CherokeeJack47

      143




      143
























          1 Answer
          1






          active

          oldest

          votes


















          0














          here is the solution, you need the key of the comment



          func uploadComment(withContent content: String, withIcon icon: String, withColor color: String, withDate date: String, withUserName userName: String, withKey key: String, sendComplete: @escaping (_ status: Bool) -> ()){
          REF_FEEDMESSAGES.child(key).child("comments").childByAutoId().updateChildValues(["content" : content, "color" : color, "icon" : icon, "date" : date, "userName" : userName])
          sendComplete(true)
          }





          share|improve this answer





















            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%2f53439689%2fuploading-comments-to-firebase-database-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









            0














            here is the solution, you need the key of the comment



            func uploadComment(withContent content: String, withIcon icon: String, withColor color: String, withDate date: String, withUserName userName: String, withKey key: String, sendComplete: @escaping (_ status: Bool) -> ()){
            REF_FEEDMESSAGES.child(key).child("comments").childByAutoId().updateChildValues(["content" : content, "color" : color, "icon" : icon, "date" : date, "userName" : userName])
            sendComplete(true)
            }





            share|improve this answer


























              0














              here is the solution, you need the key of the comment



              func uploadComment(withContent content: String, withIcon icon: String, withColor color: String, withDate date: String, withUserName userName: String, withKey key: String, sendComplete: @escaping (_ status: Bool) -> ()){
              REF_FEEDMESSAGES.child(key).child("comments").childByAutoId().updateChildValues(["content" : content, "color" : color, "icon" : icon, "date" : date, "userName" : userName])
              sendComplete(true)
              }





              share|improve this answer
























                0












                0








                0






                here is the solution, you need the key of the comment



                func uploadComment(withContent content: String, withIcon icon: String, withColor color: String, withDate date: String, withUserName userName: String, withKey key: String, sendComplete: @escaping (_ status: Bool) -> ()){
                REF_FEEDMESSAGES.child(key).child("comments").childByAutoId().updateChildValues(["content" : content, "color" : color, "icon" : icon, "date" : date, "userName" : userName])
                sendComplete(true)
                }





                share|improve this answer












                here is the solution, you need the key of the comment



                func uploadComment(withContent content: String, withIcon icon: String, withColor color: String, withDate date: String, withUserName userName: String, withKey key: String, sendComplete: @escaping (_ status: Bool) -> ()){
                REF_FEEDMESSAGES.child(key).child("comments").childByAutoId().updateChildValues(["content" : content, "color" : color, "icon" : icon, "date" : date, "userName" : userName])
                sendComplete(true)
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 23 '18 at 6:03









                Noah Iarrobino

                949




                949






























                    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.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • 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%2f53439689%2fuploading-comments-to-firebase-database-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

                    Berounka

                    Sphinx de Gizeh

                    Different font size/position of beamer's navigation symbols template's content depending on regular/plain...