Shirley Charlin Lee
Shirley Charlin Lee

Reputation: 97

Unable to get keys from firebase database

I have been pondering for the longest time in my student programmer life. I would like to know

I added the keys using autoChildId.

  1. How to get keys from firebase database swift 2? I know how to get from Android using .getKeys()

  2. My best friend, Google, taught me to use allKeys. However, my friendship is on the verge of in despair right now as I received the following msg that our relationship with .allKeys will always fail ( see image below). Haish...

Evidence of our relationship status right now

  1. I need this in order to show the data from Firebase Database into my tableview cos I believe this is the issue to a empty table just like how my heart is for my project. No heart.

Here is how my firebase database looks like:

My firebase database

Here is my code:

func findPlaceToEat(){
    print("inside findPlaceToEat()")

    print("Plan price level")
    print(planPriceLevel)
    print("End of price level")

    ref = FIRDatabase.database().reference()

    ref.child("places_detail").child("price_level").child(planPriceLevel).observeEventType(.ChildAdded, withBlock:{
        (snapshot) in

        if let dictionary = snapshot.value?.allKeys! as? [String: AnyObject]{
            let PlaceObj = placeObj(place_name: dictionary["place_name"] as! String, place_type: dictionary["place_type"] as! String, price_range: dictionary["price_range"] as! String, vegan_type:dictionary["vegan_type"] as! String , website: dictionary["website"] as! String)
            print("Whatever")

            print(PlaceObj);
            //self.tableView.reloadData()

        }
        }, withCancelBlock: nil)
}

Upvotes: 0

Views: 743

Answers (3)

moa101
moa101

Reputation: 11

You need to define query orderbykey like bellow:

  this.afd.list('/yourItems/', {query:{orderByKey :true}}).subscribe((elements) => {

            elements.map(element=>{
              console.log(element.$key);

            })

        });

Upvotes: 0

Shirley Charlin Lee
Shirley Charlin Lee

Reputation: 97

I got a workaround for my project, everyone please pray that my lecturer don't see this. :

What I did was inside the save button I retrieve the value from database and then save it back into Firebase Database.

ref = FIRDatabase.database().reference()


    ref.child("hello").child("google! I need a part time job").child(planPriceLevel).observeEventType(FIRDataEventType.ChildAdded, withBlock:{
        (snapshot: FIRDataSnapshot) in

        if let dictionary = snapshot.value as? [String: AnyObject]{
            let getPlaceObj = placeObj()

            getPlaceObj.setValuesForKeysWithDictionary(dictionary)
            self.PlaceObj.append(getPlaceObj)


            print("Name " ,getPlaceObj.place_name)


        }

        let place_name = snapshot.value?.objectForKey("place_name") as! String
        let place_type = snapshot.value?.objectForKey("place_type") as! String
        let price_range = snapshot.value?.objectForKey("price_range") as! String
        let vegan_type = snapshot.value?.objectForKey("vegan_type") as! String
        let website = snapshot.value?.objectForKey("website") as! String

        print(place_name, place_type, price_range, vegan_type, website)

        let savePlan : [String: AnyObject] = ["place_name":place_name, "place_type":place_type, "price_range":price_range, "vegan_type":vegan_type, "website":website]

        self.ref.child("can you place hire me as your intern? I am from Singapore!!!").child(self.user!.uid).childByAutoId().setValue(savePlan)


        }, withCancelBlock: nil)

Upvotes: 0

rv7284
rv7284

Reputation: 1102

to get key from snapshot

snapshot.key

Upvotes: 1

Related Questions