【问题标题】:How to remove a GeoFire handle for the following observe in Swift?如何在 Swift 中为以下观察删除 GeoFire 句柄?
【发布时间】:2017-01-03 18:21:59
【问题描述】:

这是一个针对 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

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

【问题讨论】:

    标签: ios swift firebase geofire


    【解决方案1】:

    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)
    

    【讨论】:

    • 非常感谢您的回复。刚才试过了,好像没什么区别。我试过: var locationHandle: FIRDatabaseHandle? var locationHandle: FIRDatabaseHandle! var locationHandle = FIRDatabaseHandle() var locationHandle: FIRDatabaseHandle = 0
    • @PeterdeVries 在我的答案中添加了一些快速示例代码,经过测试。
    • 非常感谢,事实上我在其他地方也有相同的结构,效果很好。问题在于移除 GeoFire 手柄。 removeAllObservers() 有效, removeObserver(withHandle: locationHandle) 无效。
    • @PeterdeVries 哎呀,我打字很快。请使用 removeObserverWithFirebaseHandle 而不是 removeObserverWithHandle。
    • 我得到:“FIRDatabaseReference”类型的值没有成员“removeObserverWithFirebaseHandle”。试图将其更改为 removeObserver(WithFirebasehandle: locationHandle),但 swift 要求我像以前一样放置 (removeObserver(withHandle)
    猜你喜欢
    • 2018-01-06
    • 1970-01-01
    • 2020-06-25
    • 2017-12-24
    • 2015-04-25
    • 1970-01-01
    • 1970-01-01
    • 2018-03-17
    相关资源
    最近更新 更多