Nivix
Nivix

Reputation: 181

How to retrieve certain childByAutoId() key

I have looked around for a while, with no luck. I need to get the string value of a certain dictionary key in firebase.

If you look at the image below,I need that key on top, and need to set it equal to a string, that I can segue to other viewcontrollers so when a user wants to make a post under the sharers for example, I go into the right value based on the key, then to sharers, where I can add values. Thanks, also I do not need all values, just once I have observedWithSingleEvent, I need to get the key of each page or dictionary.

enter image description here

my code:

    let ref = FIRDatabase.database().reference()
    ref.child("Notes").queryOrderedByKey().observeSingleEvent(of: .value, with: { snapshot in
        if let pagers = snapshot.value as? [String : AnyObject] {
           let numb = snapshot.key //what I want
            for (_, val) in pagers {
                if let useri = val["postUsername"] as? String {
                    if useri == FIRAuth.auth()?.currentUser?.uid {

                        let bindfl = Page()
                        if let title = val["title"] as? String, let descript = val["description"] as? String,
                            let sharers = val["sharers"] as? [String], let poster = val["postUsername"] as? String, let setter = val["setter"] as? String, let creatorName = val["creatorName"] as? String {
                            bindfl.title = title
                            bindfl.descriptions = descript
                            bindfl.setter = setter
                            bindfl.sharers = sharers
                            bindfl.usernameOfBinder = poster
                            bindfl.creatorName = creatorName
                            bindfl.theBit = numb

                            self.pages.append(bindfl)

Upvotes: 1

Views: 1021

Answers (2)

Dustin Spengler
Dustin Spengler

Reputation: 7761

ref.child("Notes"). observeSingleEvent(of: .childAdded, with: {snapshot in
   print("\(snapshot.key)")
})

Will give you the autoID key of every entry in the table

ref.child("Notes").queryOrdered(byChild: "creatorName").queryEqual(toValue: "posterName").observeSingleEvent(of: .childAdded, with: {snapshot in
     var postID = snapshot.key

  //update post different post using retrieved ID
     let infoToAdd = ["newPostStuff" : true]
     FIREBASE_REF!.child("Posts/\(postID)").updateChildValues(infoToAdd)
})

Will give you the auto ID of every post by a given creator and then update values on a different table with the retrieved key

Upvotes: 0

UmarZaii
UmarZaii

Reputation: 1351

Well you can do it like this, when you add data to the database

let key = ref.("Notes").childByAutoId().key
let notes = ["userName": userName,
            "description": description,
            "title": title,
            "author": author,
            "keyID": key]
let childUpdates = ["/Notes/\(key)": notes]
ref.updateChildValues(childUpdates)

After that you can, get the key like this

bindfl.title = title
bindfl.descriptions = descriptions
bindfl.userName = userName
bindfl.author= author
bindfl.keyID= keyID

Hope it worked.

Upvotes: 1

Related Questions