Completion handler Firebase observer in Swift

后端 未结 4 1477
日久生厌
日久生厌 2021-01-17 01:11

I am making a completion handler for a function which will return a list of objects. When it return value for first time, it works well. But when any change happen into fire

4条回答
  •  温柔的废话
    2021-01-17 01:21

    func ListenForChildrenAdded() {
    
    let registerToListenTo = "YourPathHere"
    
    ref.child(registerToListenTo).observeSingleEvent(of: .value) { (snapshot) in
        
        let initialChildren = snapshot.childrenCount
        var incrementer = 0
        
        ref.child(registerToListenTo).observe(.childAdded, with: { (snapshot) in
            
            incrementer += 1
            print("snapshot: \(snapshot.key) #\(incrementer)")
    
            if incrementer == initialChildren {
                print("-> All children found")
            } else if incrementer > initialChildren {
                print("-> Child Was Added - Run Some Code Here")
            }
            
        })
    }}
    

提交回复
热议问题