本文介绍了如何在Swift中观察下面的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 

重写func viewDidDisappear(_ animated:Bool){
super.viewDidDisappear(动画)

//以下不起作用:
locationsEnd?.firebaseRef.removeObserver(withHandle:locationHandle) (_ location:CLLocation){

locationHandle = locationsEnd!.query(at:location,withRadius:16.0).observe(GFEventType。 init(rawValue:0)!,与:在

等等
))
}

$ b $中的{(key,location) b

我已经尝试了locationHandle,如下所示,没有成功:

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

任何建议都会很好,正如前面提到的,我可以删除所有的观察者,但是在其他地方我需要删除一个句柄。

解决方案locationHandle在代码中被定义为一个UInt,它需要是一个FIRDatabaseHandle,所以

下面是一个移除Firebase句柄的例子

  var myPostHandle:FIRDatabaseHandle? 
$ b $ func someFunc()
{
myPostHandle = ref.child(posts)。observeEventType(.childAdded,
withBlock:{(snapshot) - >无效

让postBody = snapshot.value![body] as!String
$ b $)
}

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

对于GeoFire而言,更像是这样的

  let geoFire = GeoFire(firebaseRef:geofireRef)
let center = CLLocation(纬度:37.7832889,经度:-122.4056973)
var circleQuery = geoFire.queryAtLocation(center,withRadius:0.6)

var queryHandle = circleQuery.observeEventType(.KeyEntered,$ b $ withBlock:{(key:String !, location:CLLocation!)in
//做某事
})

然后删除,使用

  circleQuery.removeObserverWithFirebaseHandle(queryHandle)


This is a question for Swift, Firebase and Geofire.

I would like to know how to remove a GeoFire handle for the following observer in Swift.

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

The following works fine (in viewDidDisappear):

locationsEnd?.firebaseRef.removeAllObservers()

However with handle it does not:

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

I've tried the locationHandle as follows, without success:

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

Any suggestions would be great, as mentioned I can just remove all observers, but elsewhere I need to just remove a handle.

解决方案

locationHandle is defined as an UInt in your code and it needs to be a FIRDatabaseHandle so

Here's an example of removing a Firebase handle

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

For GeoFire it's more like this

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

then to remove, use the

circleQuery.removeObserverWithFirebaseHandle(queryHandle)

这篇关于如何在Swift中观察下面的GeoFire句柄?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 20:11