Uploading comments to firebase database with swift
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)
}
}
ios swift firebase firebase-realtime-database
add a comment |
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)
}
}
ios swift firebase firebase-realtime-database
add a comment |
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)
}
}
ios swift firebase firebase-realtime-database
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)
}
}
ios swift firebase firebase-realtime-database
ios swift firebase firebase-realtime-database
edited Nov 23 '18 at 1:42
KENdi
5,7092821
5,7092821
asked Nov 23 '18 at 1:30
CherokeeJack47
143
143
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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)
}
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%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
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)
}
add a comment |
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)
}
add a comment |
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)
}
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)
}
answered Nov 23 '18 at 6:03
Noah Iarrobino
949
949
add a comment |
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.
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.
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%2f53439689%2fuploading-comments-to-firebase-database-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