firebase / firebase/geofire-objc

query observers seem to be retaining their keys somehow

Open
#74 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Objective-C
Stars
447
Forks
183
PR merge metrics
No merged PRs in 30d

Description

Inside a "manager" class, I'm creating two observers (one fore each .keyEntered and .keyExited) using a region query:

```swift
internal final class GeofireManager: NSObject {

internal var locationsRef: FIRDatabaseReference = FIRDatabase.database().reference(withPath: FirebaseRoutes.locations)
internal var geoFire: GeoFire!

weak var geofireDelegate: GeoFireRequestManagerDelegate!

var regionQuery: GFRegionQuery?
var regionKeyEntered: FirebaseHandle?
var regionKeyExited: FirebaseHandle?

// ...

internal func setGeofireObserver(withRegion region: MKCoordinateRegion) {
guard let gfd = self.geofireDelegate else { return }

regionQuery = geoFire.query(with: region)

regionKeyEntered = regionQuery!.observe(.keyEntered, with: { (key, location) in
guard let currentKey: String = key else { return }
let requestsRef: FIRDatabaseReference = FIRDatabase.database().reference(withPath: FirebaseRoutes.requests)
requestsRef.child(currentKey).observeSingleEvent(of: .value, with: { (snapshot) in
gfd.geofireAdded(snapshot: snapshot)
})
})

regionKeyExited = regionQuery!.observe(.keyExited, with: { (key, location) in
guard let currentKey: String = key else { return }
gfd.geofireRemoved(key: currentKey)
})
}

// ...

}
```

My delegate calls (`gfd.geofireAdded(snapshot:)` and `gfd.geofireRemove(key: currentKey)`) are working great... but during the lifecycle of my app from the viewController that is initializing this manager, I call the following function (and set the manager to nil) to remove the observers and deinit the manager class:

```swift
internal func removeGeofireObservers() {
if regionQuery != nil {
if regionKeyEntered != nil {
regionQuery?.removeObserver(withFirebaseHandle: regionKeyEntered!)
regionKeyEntered = nil }

if regionKeyExited != nil {
regionQuery?.removeObserver(withFirebaseHandle: regionKeyExited!)
regionKeyExited = nil }

regionQuery?.removeAllObservers()
regionQuery = nil
}

if circleQuery != nil {
circleQuery?.removeAllObservers()
circleQuery = nil
}
}
```

I've verified that the `deinit {}` method is called on the manager object and I would expect that the observers and all their data would have gone away in this process... but shortly later when I reinitialize the manager object and setup the observers again, none of the `.keyEntered` observers are fired **UNLESS** I move the map and update the region (at which point `.keyExited` is fired on all the items that were there before) and then move back and update to the previous region (at which point `.keyEntered` is fired again)...

Am I doing something wrong here? Why when I remove the observers and even deinit the parent class, does the data not totally refresh when I try it again at a later time?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.