olle
olle

Reputation: 144

Swift4, Firebase - Retrieve only value of key and put in array

I want to retrieve all the values of 'itemname' from Firebase and put them in my array generatedObjects. Now it seems like i get the parent, and also the key and value of its children, when i only want all values of 'itemname' ('Toiletry bag', 'Toothbrush' etc.). Not sure where to go from here, appreciate any help:

enter image description here

enter image description here

My code:

self.ref.child("lists").child("-LOXr5PoUvBn_tGNhql-").child(whatList!).observeSingleEvent(of: .value, with: { (snapshot) in
            let children = snapshot.children
            while let rest = children.nextObject() as? DataSnapshot{
                print(rest.value)
                //self.generatedObjects.append(rest.value as! [String: AnyObject])
            }

        },withCancel: nil)

Upvotes: 0

Views: 37

Answers (2)

elbert rivas
elbert rivas

Reputation: 1464

Try this

func getData() {
   ref.child("lists").child("-LOXr5PoUvBn_tGNhql-").child("mylist").observe(.childAdded) { (snapshot) in
        let result = snapshot.value as? [String: Any]

        let item = result!["itemname"]
        //append item to your array
        ...
    }
}

Upvotes: 1

jagdeep singh
jagdeep singh

Reputation: 156

func fetchdata(toId: String)
{
    Database.database().reference().child("Users").child(toId).observeSingleEvent(of: .value) { (snapshot) in
        guard let dictionary = snapshot.value as? [String:Any] else {return}
        let itemname = dictionary["itemname"] as! String

        print(itemname)

    }
}

does it helpfull!!

Upvotes: 0

Related Questions