【问题标题】:Firebase,Swift: Retrieving only those `autoID` node's which have a specific keyFirebase,Swift:仅检索那些具有特定键的“autoID”节点
【发布时间】:2016-09-09 21:30:58
【问题描述】:

问题--->

  1. 有没有办法检索键为autoID 的节点,该节点可能包含特定键。就像在下面的 JSON 结构中,我在一些 autoID 中有一个子节点 powers不是全部,我只想检索那些不知道其中有键 powers 的节点对应的value 可能是什么。

  2. 在以下两种建议的方法中,哪一种会消耗更少的带宽

我的 JSON 树

node1   
  -node12  
    -autoId1  

        expo: "5122223333"
        users:   
            -MqrvHRBTRcPzrvAOdkklBzeFW7E2  
                firstName: "Margery"  
                lastName: "Lady" 
    -autoId2  
        powers: "Triple3"
        expo: "2123338983"
        users:   
            -LqrsadaDs12BTRcPzrvABzeFW7E2  
                firstName: "Tyrion"  
                lastName: "Imph"
  -node21  
    -autoId3  
        powers: "Triple"  
        expo: "5123333"
        users:   
            -MqrvHRBTRcPzrvAOdkklBzeFW7E2  
                firstName: "Cersie"  
                lastName: "Lady" 
    -autoId4  
        powers: "Quad"  
        expo: "2128983"
        users:   
            -LqrsadaDs12BTRcPzrvABzeFW7E2  
                firstName: "Sansa"  
                lastName: "Lady"  

我尝试了什么--->

  • 检索整个node12,然后检查哪些autoId 具有特定的key。例如让我们说权力:“Triple3”

    let prntRef = FIRDatabase.database().reference().child("node1").child("node12")
    prntRef.observeSingleEventOfType(.Value, withBlock: {(snap) in
    
      if snap.exists(){
    
         for each in snap.value as! [String:AnyObject]{
    
                 prntRef.child(each.0 as! String).child("powers").observeSingleEventOfType(.Value, withBlock: {(IMsnap) in
    
               if IMsnap.exists(){
    
                    //Found The correct node
                    }
                })
            }
          }else{
    
             //
            }
       })
    
  • 我的另一个替代解决方案是:-

    FIRDatabase.database().reference().child("node1").child("node12").queryOrderedByChild("powers").observeSingleEventOfType(.Value, withBlock: {(snap) in
    
       if let snapDict = snap.value! as? [String : AnyObject]{
    
               print(snapDict.keys.first!)   //Retrieving My AutoID .Nut this gives me entire node. 
    
            }
    
            for each in snap.value as! [String:AnyObject]{
    
                 print(each.0)   //Retrieving My AutoID 
    
           }
       })
    
    })
    

注意:-我在 Firebase 论坛上发现了一些类似的 Q,但似乎没有人回答它:-https://groups.google.com/forum/#!topic/firebase-talk/ZDHKwxRMiKQ

【问题讨论】:

    标签: ios swift firebase swift2 firebase-realtime-database


    【解决方案1】:

    如果你不关心powers的值,你只关心key是否存在于autoId节点下,你只需要.queryEqualToValue("")(这取决于key-value对的值是a字符串,对于数字,而不是 "" 只需使用 0 [取决于您的值是否大于或等于 0])。

    let ref = FIRDatabase.database().referenceWithPath("node1/node12")
    
    ref.queryOrderedByChild("powers")
    ref.queryEqualToValue("")
    ref.observeEventOfType(.Value, withBlock: { snap in
    
        print(snap) // all the autoId nodes that have the powers key
    })
    

    【讨论】:

      猜你喜欢
      • 2017-12-05
      • 1970-01-01
      • 1970-01-01
      • 2019-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多