Firebase:如何从子节点swift中提取子键值

时间:2016-09-17 18:26:48

标签: swift firebase

如何提取孩子的ID值(即followerID1,followerID2 ......)? firebase的结构如下:

Followers:{
    USER_ID:{
        followerID1 = false;
        followerID2 = true;
        followerID3 = true;
    }
}

我在xCode中执行以下操作,但它显示了USER_ID下的整个节点:

self.databaseRef.child("Followers").child(self.USER_ID).Value, withBlock: { (snapshot:FIRDataSnapshot) in
          self.userFollowDump = snapshot
 })

谢谢!我是firebase的新手:)

1 个答案:

答案 0 :(得分:5)

试试这个: -

  • Swift 3

    FIRDatabase.database().reference().child("Followers").child(FIRAuth.auth()!.currentUser!.uid).observeSingleEvent(of: .value, with: { (snapshot) in
            if let userDict = snapshot.value as? [String:AnyObject]{
                for each in userDict{
                    let follwerID = each.0 as! String // Every follwers ID.
                }
            }
        })
    

    如果您正在寻找值 true followerId

        FIRDatabase.database().reference().child("Followers").child(FIRAuth.auth()!.currentUser!.uid).queryOrderedByValue().queryEqual(toValue: true).observeSingleEvent(of: .value, with: {(snap) in
            if let snapDict = snap.value as? [String:AnyObject]{
                for each in snapDict{
    
                    print(each.key)
                }
            }
        })
    
  • Swift 2

    FIRDatabase.database().reference().child("Followers").child(FIRAuth.auth()!.currentUser!.uid).observeSingleEventOfType(.Value, withBlock: { (snapshot) in
     if let userDict = snapshot.value as? [String:AnyObject]{
          for each in userDict{
             let follwerID = each.0 as! String // Every follwers ID.
             }
         }
      })
    

    但如果您正在寻找 true

    followerId
    FIRDatabase.database().reference().child("Followers").child(FIRAuth.auth()!.currentUser!.uid).queryOrderedByValue().queryEqualToValue(true).observeSingleEventOfType(.Value, withBlock: {(snap) in
         if let snapDict = snap.value as? [String:AnyObject]{
           for each in snapDict{
    
                print(each.key)
            }
        }
    })