如何在Firebase中检索子快照?

时间:2016-10-27 09:15:14

标签: swift firebase firebase-realtime-database

我正在尝试使用childSnapshot在firebase中创建for child in snapShot.children我知道我曾经能够使它工作,我不确定这是否是新更新中的更改

以前的工作方式

 for child in snapshot.children {                   
                        let firUserId = child.childSnapshot(forPath: "userId").value! as! String
                    let receivePostSnapshot = child.childSnapshot(forPath: "receivePost")
    }

我当前的代码不起作用

func displayAvailaibleHelprOnMap(){
let refUrl = "frontEnd/users"
    let childRef = Ref.childRef(refUrl: refUrl)//.child("users")
    childRef.observe(.value, with: {snapshot in
    let dict = snapshot.value as? NSDictionary

     for child in snapshot.children{
// I want to use child snapshot here
//let childDict = child.childSnapshot(forPath: "receivePost")
 }

}

3 个答案:

答案 0 :(得分:4)

尝试:

  var ref: FIRDatabaseReference!
  ref = FIRDatabase.database().reference()
  ref.child("users").observeSingleEvent(of: .value, with: { (snapshot) in
            if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {

                for snap in snapshots
                {
                    let firUserId = child.childSnapshot(forPath: "userId").value! as! String
                    let receivePostSnapshot = child.childSnapshot(forPath: "receivePost")  
                }
            }
        })

答案 1 :(得分:0)

试试这个

let mRef = mRestCallObj.getReferenceFirebase()
                          mRef!.child("users").queryOrderedByChild("receivePost").observeEventType(.ChildAdded, withBlock: { snapshot in
        let userName = snapshot.value!["receivePost"] as? String

}

答案 2 :(得分:0)

试试这个 -

    let ref = FIRDatabase.database().reference().child("Users + infomation").child(userid).child("cats")`
ref.observeSingleEventOfType(.Value, withBlock : {(snapShot) in

        if let val = snapShot.value as? Int {print(snapShot)}