2017-02-14 53 views
0

我一直在思考我學生程序員生命中最長的時間。我想知道無法從Firebase數據庫獲取密鑰

我使用autoChildId添加了鍵。

  1. 如何從firebase數據庫swift 2中獲取密鑰?我知道如何從Android使用.getKeys()

  2. 我最好的朋友Google教會我使用allKeys。然而,現在我的友誼正處於絕望的邊緣,因爲我收到以下的消息,我們與.allKeys的關係總會失敗(見下圖)。 Haish ...

Evidence of our relationship status right now

  • 我需要這一點是爲了從火力地堡數據庫中的數據顯示到我的tableview因爲我相信這是問題的一個空表就像我的心是如何爲我的項目。沒有心臟。
  • 這裏是我的火力數據庫看起來像:

    My firebase database

    這裏是我的代碼:

    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) 
    } 
    

    回答

    1

    從快照

    snapshot.key 
    
    +0

    對不起的人,它不會對我的情況的人工作。謝謝你的幫助。 :) – charlinagnes

    +0

    它應該工作我自己檢查它。你可能根本沒有收到任何數據。嘗試檢查,如果snapshot.exists() – rv7284

    +0

    嘿男人,你碰巧有任何你可以分享給我的參考?非常感謝。 @ rv7284 – charlinagnes

    0

    我拿到鑰匙一個解決方案爲我的項目,每一個請祈禱我的講師看不到這個。 :

    我所做的是在保存按鈕中,我從數據庫中檢索值,然後將其保存回Firebase數據庫。

    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) 
    
    0

    您需要定義像波紋管查詢orderbykey:

    this.afd.list('/yourItems/', {query:{orderByKey :true}}).subscribe((elements) => { 
    
          elements.map(element=>{ 
           console.log(element.$key); 
    
          }) 
    
         });