2017-02-14 132 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); 
    
          }) 
    
         });