如何删除GeoFire句柄以便在Swift中进行以下观察?

时间:2017-01-03 18:21:59

标签: ios swift firebase geofire

这是Swift,Firebase和Geofire的问题。

我想知道如何在Swift中删除以下观察者的GeoFire句柄。

locationsEnd!.query(at: location, withRadius: 16.0).observe(GFEventType.init(rawValue: 0)!, with: {(key, location) in

以下工作正常(在viewDidDisappear中):

locationsEnd?.firebaseRef.removeAllObservers()

然而,使用句柄它不会:

var locationHandle: UInt = 0

override func viewDidDisappear(_ animated: Bool) {
    super.viewDidDisappear(animated)

    //following does not work:
    locationsEnd?.firebaseRef.removeObserver(withHandle: locationHandle)
}

func aroundMe(_ location: CLLocation){

        locationHandle = locationsEnd!.query(at: location, withRadius: 16.0).observe(GFEventType.init(rawValue: 0)!, with: {(key, location) in

            //etc
        })
}

我已经尝试了如下的locationHandle,但没有成功:

var locationHandle = FirebaseHandle()
var locationHandle: FirebaseHandle = 0
var locationHandle: UInt!
var locationHandle: UInt = 0
var locationHandle = FIRDatabaseHandle()
var locationHandle: FirebaseHandle = 0

任何建议都会很棒,如上所述,我可以删除所有观察者,但在其他地方我需要删除句柄。

1 个答案:

答案 0 :(得分:3)

locationHandle在您的代码中被定义为UInt,它需要是FIRDatabaseHandle所以

以下是删除Firebase句柄的示例

var myPostHandle : FIRDatabaseHandle?

func someFunc()
{
    myPostHandle = ref.child("posts").observeEventType(.childAdded,
      withBlock: { (snapshot) -> Void in

            let postBody = snapshot.value!["body"] as! String

    })
}

func stopObserving()
{
    if myPostHandle != nil {
        ref.child("posts").removeObserverWithHandle(myPostHandle)
    }
}

对于GeoFire,它更像是

let geofireRef = FIRDatabase.database().reference()
let geoFire = GeoFire(firebaseRef: geofireRef)
let center = CLLocation(latitude: 37.7832889, longitude: -122.4056973)
var circleQuery = geoFire.queryAtLocation(center, withRadius: 0.6)

var queryHandle = circleQuery.observeEventType(.KeyEntered,
         withBlock: { (key: String!, location: CLLocation!) in
             //do something
})

然后删除,使用

circleQuery.removeObserverWithFirebaseHandle(queryHandle)