2016-10-27 150 views
1

我試着使用for child in snapShot.children我知道當時我曾經是能夠使它發揮作用創造一個火力點childSnapshot,林不知道這是否是在新的更新如何檢索Firebase中的子快照?

是用來工作

-old方式的變革
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") 
} 

} 
+0

如果*我當前的代碼不工作*,到底是你的問題或錯誤? – Dravidian

+0

@Dravidian它應該說不行 – pprevalon

回答

3

嘗試:

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

試試這個

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

}

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