For some reason when I use GeoFire to pull events it never seems to stop pulling events. I noticed when I left my computer for a little and came back and my array had 1000 elements in it.
static func showEvent(pageSize: UInt, lastPostKey: String? = nil, category: String? = nil,completion: @escaping ([Event]) -> Void) {
//array that will hold events
var currentEvents = [Event]()
var currentEventKeys = [String]()
//getting firebase root directory
//create geofire ref to get current user location
let geofireRef = Database.database().reference().child("userlocations")
//create new geofire instance to actually pull event based off location
//create geofire instance to handle the ref
let geoFire = GeoFire(firebaseRef: geofireRef)
let eventgeofireRef = Database.database().reference().child("eventlocations")
let geoFire1 = GeoFire(firebaseRef: eventgeofireRef)
//will get the location for of the current user from database
//
geoFire?.getLocationForKey(Auth.auth().currentUser?.uid, withCallback: { (location, error) in
if (error != nil) {
print("An error occurred getting the location for \"firebase-hq\": \(String(describing: error?.localizedDescription))")
} else if (location != nil) {
print("Location for \(String(describing: Auth.auth().currentUser?.uid)) is [\(String(describing: location!.coordinate.latitude)), \(String(describing: location!.coordinate.longitude))]")
//safely unwrapping user location
guard let userLocation = location else{
return
}
//will create a circle query to check for event keys within 600 meters of current user location
let circleQuery = geoFire1?.query(at: userLocation, withRadius: 2.0)
//will use that query to fetch results from database
var queryHandle = circleQuery?.observe(.keyEntered, with: { (key: String!, location: CLLocation!) in
// print("Key '\(key)' entered the search area and is at location '\(location)'")
print(key)
currentEventKeys.append(key)
for currentKey in currentEventKeys {
EventService.show(forEventKey: currentKey, eventCategory: "Home", completion: { (event) in
currentEvents.append(event!)
completion(currentEvents)
})
}
})
circleQuery?.observeReady({
print("All initial data has been loaded and events have been fired!")
circleQuery?.removeAllObservers()
})
} else {
print("GeoFire does not contain a location for \"firebase-hq\"")
}
})
}
Everything else seems to be working and I am removing the observer but it just doesn't stop pulling events.