Remove observer after value change in Firebase

I have global observer in ViewController and need some different observers inside it for specific values like one below. Is it possible to remove observer after value change once?

var ref = Firebase(url: "https://<FIREBASE-APP>")

let handle = ref.observeEventType(.Value, withBlock: { snapshot in

    //Here VALUE Changes to NEW_VALUE
    if snapshot.value as! String == NEW_VALUE {

        ...something here



  • How to check if a uiimage is blank? (empty, transparent)
  • Is there a way to ask user for Camera access after they have already denied it on iOS 8?
  • How to change cordova-camera-plugin language in iOS?
  • What's the difference between performSelectorOnMainThread and dispatch_async on main queue?
  • AirPlay button on custom view
  • Underline text in a UITextView
  • Solutions Collect From Internet About “Remove observer after value change in Firebase”

    This is one of the cases where you need to take an extra step in Swift, since it doesn’t realize that you can safely access handle inside the block.

    One way of working around this is:

    let ref = Firebase(url: "")
    var handle: UInt = 0
    handle = ref.observeEventType(.Value, withBlock: { snapshot in
        if snapshot.exists() && snapshot.value as! String == "42" {
            print("The value is now 42")

    By explicitly initializing the handle variable, we remove the error from the Swift compiler. But given that the handle will have been set before our block is invoked, we can safely call ref.removeObserverWithHandle(handle) inside the block.