4

I have a function that shows pins on a MKMapView based on the user's current location. This function utilizes GeoFire's GFQuery:

// Allows the user to scroll on the map and have the parties update
func mapView(_ mapView: MKMapView, regionWillChangeAnimated animated: Bool) {
    let loc = CLLocation(latitude: mapView.centerCoordinate.latitude, longitude: mapView.centerCoordinate.longitude)

    showPartiesOnMap(location: loc)

}

As you can see from the snippet of code, this function repeatedly calls showParties on map (for each time the location is updated). That function looks like this:

func showPartiesOnMap(location:CLLocation) {
    circleQuery = geoFire?.query(at: location, withRadius: 2.5)

    // Observe whenever we find a sighting. If there are 50 parties, this will be called 50 times
    partyObserve = circleQuery?.observe(GFEventType.keyEntered, with: { (key, location) in

        if let location = location, let key = key {

            let anno: PartyAnnotation


            self.fullPartyName = key

            // Attempting to print just the name of the party and not the time
            var characters = Array(key.characters)

            var currentChar: Character = " "
            var finalName: String = ""
            var dateString: String = ""
            var finalDate: Date? = nil


            // Gets the name of the party
            currentChar = characters.remove(at: 0)
            while currentChar != "*" {

                finalName += "\(currentChar)"
                currentChar = characters.remove(at: 0)
            }

            // Gets the date the party was created
            currentChar = characters.remove(at: 0)
            while currentChar != "*" {
                dateString += "\(currentChar)"
                currentChar = characters.remove(at: 0)
            }

            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
            finalDate = dateFormatter.date(from: dateString)!

            // Check if the party should still exist
            if(self.shouldBeDeleted(partyDate: finalDate!)) {
                self.geoFire.removeKey(key)
            }
            else if characters.count > 0 {
                // The party in question is public
            }
            else {
                // Created an annotation because the party should not be deleted
                anno = PartyAnnotation(coordinate: location.coordinate, partyName: finalName)

                self.mapView.addAnnotation(anno)

            }

        }

    })

}

The issue that I am facing is that when the user leaves this page, I am no longer able to properly observe anything on Firebase. For any future observes (non-GeoFire), I receive an unexpectedly found nil while unwrapping an optional value.

After a bunch of testing, I have concluded that the issue comes from the line:

partyObserve = circleQuery?.observe(GFEventType.keyEntered, with: { (key, location) in

If I comment this observation out, the problem does not happen. Unfortunately, I need this functionality so commenting it out is not a viable solution. I have attempted removingAllObservers and removing observes with Firebase Handles. Does anyone know why this may be happening or a possible solution?

Thanks in advance for the help!

Mitch Kelly
  • 481
  • 3
  • 10

0 Answers0