Firebase,Swift:仅检索那些具有特定键的“自动识别号”节点 [英] Firebase,Swift: Retrieving only those `autoID` node's which have a specific key

查看:164
本文介绍了Firebase,Swift:仅检索那些具有特定键的“自动识别号”节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法来检索一个节点,其中的关键是 autoID ,其中 Might 包含特定的键。就像在下面的JSON结构中,我有一个子节点 powers 在一些 autoID 中,不是所有的,我只想检索那些不知道对应的可能有 是的。在下面两个建议的方法中,哪一个会消耗更少的带宽?


我的JSON树

  node1 
-node12
-autoId1

expo:5122223333
users:
-MqrvHRBTRcPzrvAOdkklBzeFW7E2
姓氏:Margery
姓氏:Lady
-autoId2
权力:Triple3
展示:2123338983
用户:
-LqrsadaDs12BTRcPzrvABzeFW7E2
姓氏:Tyrion
姓氏:Imph
-node21
-autoId3
功率:Triple
expo:5123333
用户:
-MqrvHRBTRcPzrvAOdkklBzeFW7E2
名字:Cersie
姓氏:Lady
-autoId4
功率:四元
expo:2128983
用户:
-LqrsadaDs12BTRcPzrvABzeFW7E2
名字:Sansa
lastName:Lady


$ b


  • 检索整个 node12 ,然后检查 autoId 有一个特定的。例如让我们说权力:Triple3

      let prntRef = FIRDatabase.database()。reference()。child(node1 ).child(node12)
    prntRef.observeSingleEventOfType(.Value,withBlock:{(snap)in
    $ b $ if snap.exists(){

    每个在snap.value as![String:AnyObject] {

    prntRef.child(each.0 as!String).child(powers)。observeSingleEventOfType(.Value,withBlock:{( IMsnap.exists(){

    //找到正确的节点
    }
    })
    }

    $ b} else {

    //
    }
    })


  • 我的另一种解决方案是: - $ /
    $ b $ $ $ $ $ $ $ $ $ $ FIRDatabase.database()。 (node1)。child(node12)。queryOrderedByChild(powers)。observeSingleEventOfType(.Value,withBlock:{(snap)in
    $ b $ if snapDict = snap.value!as? [String:AnyObject] {

    print (snapDict.keys.first!)//获取我的AutoID .Nut这给了我整个节点。

    }

    为snap.value中的每个! [String:AnyObject] {

    print(each.0)//检索我的AutoID

    }
    })

    })




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

解决方案

如果你不关心 powers 的值,那么你只关心autoId节点下的key,你只需要。 queryEqualToValue()(这取决于键值对是一个字符串的值,而不是,只是使用0 [取决于您的值大于或等于)

$ p $ let ref = FIRDatabase.database()。referenceWithPath(node1 / node12)

ref.queryOrderedByChild(powers)
ref.queryEqualToV alue()
ref.observeEventOfType(.Value,withBlock:{snap in

print(snap)//所有具有电源键
的autoId节点)


Question--->

  1. Is there a way to retrieve a node whose key is an autoID, which Might contain a particular key. Like in below JSON structure i have a child node powers inside some autoID's, not all of them, I want to retrieve only those nodes which have key powers in them not knowing what corresponding value might be .

  2. In the below two suggested approach which one would consume less BandWidth?

My JSON Tree

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"  

What Have I Tried--->

  • Retrieve the entire node12 and then checking which of the autoId's have a particular key. for eg lets say powers: "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{
    
             //
            }
       })
    

  • My other alternative solution is:-

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

Note:- I found somewhat similar Q posted in Firebase forum, but no one seems to answer it :-https://groups.google.com/forum/#!topic/firebase-talk/ZDHKwxRMiKQ

解决方案

If you don't care about the value of powers, you only care that the key exists under the autoId node, you just need .queryEqualToValue("") (this is dependent on the value of the key-value pair being a string, for a number, instead of "" just use 0 [dependent on your values being greater than or equal to 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
})

这篇关于Firebase,Swift:仅检索那些具有特定键的“自动识别号”节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆