Can't fix a leak that Leaks Tool points to

78 Views Asked by At

I have a method which I use to se tup and show annotation on the map:

func setupPlacemark(place: Place, mapView: MKMapView) {

    guard let location = place.location else { return }

    let geocoder = CLGeocoder()
    geocoder.geocodeAddressString(location) { [unowned self] (placemarks, error) in

        if let error = error {
            print(error)
            return
        }

        guard let placemarks = placemarks else { return }

        let placemark = placemarks.first

        let annotation = MKPointAnnotation()
        annotation.title = place.name
        annotation.subtitle = place.type

        guard let placemarkLocation = placemark?.location else { return }

        annotation.coordinate = placemarkLocation.coordinate
        self.placeCoordinate = placemarkLocation.coordinate

        mapView.showAnnotations([annotation], animated: true)
        mapView.selectAnnotation(annotation, animated: true)
    }
}

When I run Leaks tool I see this leak:

image

And here you will see which line of code doesn’t like:

image

What I should do to make this leak go away?

1

There are 1 best solutions below

2
On

Try replacing :

mapView.showAnnotations([annotation], animated: true)
mapView.selectAnnotation(annotation, animated: true)

With :

weak var weakMapView = mapView
weakMapView.showAnnotations([annotation], animated: true)
weakMapView.selectAnnotation(annotation, animated: true)

Let me know if it helped, if so I will add an explanation.