3

I am trying to create a simple Instagram style app using Swift and Firebase and I am running into trouble reading comments for each Image post from Firebase.

Firebase Screenshot

A couple questions here:

  • I have the Posts at the top of the tree and then the keys per image under which have data on the comments added by each user. Is it possible to use the username as the key instead of the key generated by childbyAutoId in this instance ?

  • How would I read the userComment and UserName and save them into an Array that I can then display in a TableView ?

Any response is much appreciated. Thanks.

3
  • You can use child() or setValue()to set the key to whatever you want instead of childByAutoId(). See the docs Commented Aug 5, 2016 at 21:03
  • Be careful: If you use the username as the key, make sure not to overwrite comments posted by the same user! Commented Aug 5, 2016 at 22:04
  • I see what you say ( for overwriting) , but not sure how to not overwrite or if its even possible. So childBuautoId seems to be the correct solution. Commented Aug 6, 2016 at 16:06

1 Answer 1

4
var postsCommentsDict : NSMutableDictionary = NSMutableDictionary()
var userNameArray : [String] = [String]() 
var userCommentArray : [String] = [String]()
FIRDatabase.database.reference().child("Posts").observeEventType(.Value, withBlock: {(Snapshot) in

if Snapshot.exists(){
    let imageD = Snapshot.value
    let imageD_ID = imageD.key
   //Retrieving the email and image name
     let imageName = imageD["userImage"] as! String
     let userEmail = imageD["userEmail"] as! String
    //Here you are accessing each image ID
    //First Question Alternative Completed

         //To answer the second Question :-

     FIRDatabase.database.reference().child("Posts").child(imageD_ID).child("comments").observeEventType(.Value, withBlock: {(Snapshot) in

  if let commentsDictionary = Snapshot.value {

    for each in commentsDictionary{

      postsCommentsDict.setObject(each["userName"] as! String , forKey : each["userComment"] as! String)
               //Saving the userName : UserComment in a dictionary
      userNameArray.append(each["userName"] as! String)
      userCommentArray.append(each["userComment"] as! String)
               //Saving the details in arrays
               //Prefer dictionary over Arrays

    }

} else {

   //no comments to append the arrays

        }

      })

   }
})

Once you are Done Saving the Comments dictionary : How to read it : -

for each in postsCommentsDict as! NSDictionary{
  let userNm = each.key
  let userComment = each.value
  //username and userComment's retrieved 

}

Please ignore the Typos, if any!...hope this helps..

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks Dravidian for that detailed reply. I'm already saving the image and some other information such as email for the posted image in an array. I'm getting stuck at trying to figure out how I would retrieve this then from an array ( thats inside an array ).
This is how the data I retrieve looks: ** code** ` ({ "-KOLxwm7YrCBnzqpbSG2" = { comments = { "-KOVaDpglUPzWnDvokQk" = { userComment = e; userName = SB; }; }; userComment = zzz; userEmail = "[email protected]"; userImage = "/9j/4 `
Thanks Dravidian, I'll look into this and update but this already helps so much.
I keep getting errors when I use this line: postsCommentsDict.setObject(each["userName"] as! String : each["userComment"] as! String) and I only get .setObject( anObject: AnyObject, forKey:NSCopying) in Swift
Ists ays " Expected ';' separator after 'as! String :
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.