I'm trying to sort in different labels every items of a dictionary (in my exemple:vote in firebase go in the specify "votelabel", text in "textlabel" ... ), I tried to do it this way but I realy get stock. I think my problem is because every post have a new key and I don't Know How to go directly to the children
var posts: [String: AnyObject] = [String: AnyObject]()
@IBAction func save(sender: AnyObject) {
let titre = Titre.text
let soustitre = SousTitre.text
let text = Text.text
let newPosts: Dictionary<String, AnyObject> = [
"titre": titre!,
"soustitre": soustitre!,
"text": text!,
"votes": votes
]
ref.childByAppendingPath(current).childByAppendingPath("Posts").childByAutoId().setValue(newPosts)
ref.childByAppendingPath(current).observeEventType(.ChildAdded, withBlock: { snapshot in
print(snapshot.value)
from here I realy get lost, I find this part on a tutorial, but to be honnest I didn't get it.
var posts = [NSDictionary]()
for item in snapshot.children{
let child = item as! FDataSnapshot
let dict = child.value as! NSDictionary
posts.append(dict)
}
self.TextLabel.text = snapshot
})
}
any clue will be very usefull !
thank you for your time !
FireBase
Given that your Firebase structure looks like this
posts
node_0
title: "some title"
text: "some text"
vote: "some vote"
node_1
title: "another title"
text: "another text"
vote: "another vote"
node_2
title: "yet another title"
text: "yet another text"
vote: "yet another vote"
and some code to read all of the posts nodes and display their children. The snapshot returns a set of key:value pairs so use the key to access the value
let myRootRef = Firebase(url:"https://jaytest.firebaseio.com")
let postsRef = myRootRef.childByAppendingPath("posts"
postsRef.observeSingleEventOfType(.Value, withBlock { snapshot in
for child in snapshot.children {
if let title = child.value["title"] as? String {
print(title)
}
if let text = child.value["text"] as? String {
print(text)
}
if let title = child.value["vote"] as? String {
print(vote)
}
}
}
output is
some title
some text
some vote
another title
another text
another vote
yet another title
yet another text
yet another vote
based on a bit more info, the question is:
How do I retrieve specific child data from a post in Firebase.
Suppose we have a tableview that lists our posts. Each post's key (node_0, node_1 & node_2) should be stored in an array that matches the tableView (it could be used as the datasource)
When the user clicks or taps a row, row #1 for example, look up the data in the array. In this case assume they tap row 1 in the tableView:
var theKey = myArray[1] //returns a key called 'node_1'
now that we have the key, getting the data from Firebase is a 'snap'
postsRef = rootRef.childByAppendingPath("posts")
thisPostRef = postsRef.childByAppendingPath(theKey)
thisPostRef.observeSingleEventOfType(.Value withBlock { snapshot in
let title = snapshot.value["title"] //title = another title
let text = snapshot.value["text"] // text = another text
let vote = snapshot.value["vote"] //vote = another vote
}