Skip to content

Commit 572d215

Browse files
luisluis
authored andcommitted
fix: Removed retain cycles to fix memory leak
Multiple retain cycles were removed to fix a memory leak. MkMapViews are now deinitialized correctly. Fixes #13
1 parent 0497496 commit 572d215

13 files changed

+395
-277
lines changed

example/ios/Podfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ SPEC CHECKSUMS:
1919

2020
PODFILE CHECKSUM: aafe91acc616949ddb318b77800a7f51bffa2a4c
2121

22-
COCOAPODS: 1.10.1
22+
COCOAPODS: 1.10.0

ios/Classes/Annotations/AnnotationController.swift

Lines changed: 36 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,10 @@
88
import Foundation
99
import MapKit
1010

11-
class AnnotationController: NSObject {
11+
extension AppleMapController: AnnotationDelegate {
1212

13-
let mapView: MKMapView
14-
let channel: FlutterMethodChannel
15-
let registrar: FlutterPluginRegistrar
16-
17-
public init(mapView :MKMapView, channel :FlutterMethodChannel, registrar: FlutterPluginRegistrar) {
18-
self.mapView = mapView
19-
self.channel = channel
20-
self.registrar = registrar
21-
}
22-
23-
func getAnnotationView(annotation: FlutterAnnotation) -> MKAnnotationView{
24-
let identifier :String = annotation.id
13+
func getAnnotationView(annotation: FlutterAnnotation) -> MKAnnotationView {
14+
let identifier: String = annotation.id
2515
var annotationView = self.mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
2616
let oldflutterAnnoation = annotationView?.annotation as? FlutterAnnotation
2717
if annotationView == nil || oldflutterAnnoation?.icon.iconType != annotation.icon.iconType {
@@ -58,15 +48,15 @@ class AnnotationController: NSObject {
5848
return annotationView!
5949
}
6050

61-
public func annotationsToAdd(annotations :NSArray) {
51+
func annotationsToAdd(annotations :NSArray) {
6252
for annotation in annotations {
6353
let annotationData :Dictionary<String, Any> = annotation as! Dictionary<String, Any>
6454
addAnnotation(annotationData: annotationData)
6555
}
6656
}
6757

68-
public func annotationsToChange(annotations: NSArray) {
69-
let oldAnnotations :[MKAnnotation] = mapView.annotations
58+
func annotationsToChange(annotations: NSArray) {
59+
let oldAnnotations :[MKAnnotation] = self.mapView.annotations
7060
for annotation in annotations {
7161
let annotationData :Dictionary<String, Any> = annotation as! Dictionary<String, Any>
7262
for oldAnnotation in oldAnnotations {
@@ -86,30 +76,55 @@ class AnnotationController: NSObject {
8676
}
8777
}
8878

89-
public func annotationsIdsToRemove(annotationIds: NSArray) {
79+
func annotationsIdsToRemove(annotationIds: NSArray) {
9080
for annotationId in annotationIds {
9181
if let _annotationId :String = annotationId as? String {
9282
removeAnnotation(id: _annotationId)
9383
}
9484
}
9585
}
86+
87+
func removeAllAnnotations() {
88+
self.mapView.removeAnnotations(self.mapView.annotations)
89+
}
9690

97-
public func onAnnotationClick(annotation :MKAnnotation) {
91+
func onAnnotationClick(annotation :MKAnnotation) {
9892
if let flutterAnnotation :FlutterAnnotation = annotation as? FlutterAnnotation {
9993
flutterAnnotation.wasDragged = true
10094
channel.invokeMethod("annotation#onTap", arguments: ["annotationId" : flutterAnnotation.id])
10195
}
10296
}
97+
98+
func showAnnotation(with id: String) {
99+
let annotation = self.getAnnotation(with: id)
100+
guard annotation != nil else {
101+
return
102+
}
103+
self.mapView.selectAnnotation(annotation!, animated: true)
104+
}
105+
106+
func hideAnnotation(with id: String) {
107+
let annotation = self.getAnnotation(with: id)
108+
guard annotation != nil else {
109+
return
110+
}
111+
self.mapView.deselectAnnotation(annotation!, animated: true)
112+
}
113+
114+
func isAnnotationSelected(with id: String) -> Bool {
115+
return self.mapView.selectedAnnotations.contains(where: { annotation in return self.getAnnotation(with: id) == (annotation as? FlutterAnnotation)})
116+
}
117+
103118

104119
private func removeAnnotation(id: String) {
105120
if let flutterAnnotation :FlutterAnnotation = self.getAnnotation(with: id) {
106-
mapView.removeAnnotation(flutterAnnotation)
121+
self.mapView.removeAnnotation(flutterAnnotation)
107122
}
108123
}
109124

110125
private func updateAnnotationOnMap(oldAnnotation: FlutterAnnotation, newAnnotation :FlutterAnnotation) {
111126
removeAnnotation(id: oldAnnotation.id)
112-
mapView.addAnnotation(newAnnotation)
127+
self.mapView.addAnnotation(newAnnotation)
113128
}
114129

115130
private func initInfoWindow(annotation: FlutterAnnotation, annotationView: MKAnnotationView) {
@@ -133,33 +148,13 @@ class AnnotationController: NSObject {
133148
}
134149
}
135150

136-
public func showAnnotation(with id: String) {
137-
let annotation = self.getAnnotation(with: id)
138-
guard annotation != nil else {
139-
return
140-
}
141-
self.mapView.selectAnnotation(annotation!, animated: true)
142-
}
143-
144-
public func hideAnnotation(with id: String) {
145-
let annotation = self.getAnnotation(with: id)
146-
guard annotation != nil else {
147-
return
148-
}
149-
self.mapView.deselectAnnotation(annotation!, animated: true)
150-
}
151-
152-
public func isAnnotationSelected(with id: String) -> Bool {
153-
return self.mapView.selectedAnnotations.contains(where: { annotation in return self.getAnnotation(with: id) == (annotation as? FlutterAnnotation)})
154-
}
155-
156151
private func getAnnotation(with id: String) -> FlutterAnnotation? {
157152
return self.mapView.annotations.filter { annotation in return (annotation as? FlutterAnnotation)?.id == id }.first as? FlutterAnnotation
158153
}
159154

160155
private func addAnnotation(annotationData: Dictionary<String, Any>) {
161156
let annotation :MKAnnotation = FlutterAnnotation(fromDictionary: annotationData, registrar: registrar)
162-
mapView.addAnnotation(annotation)
157+
self.mapView.addAnnotation(annotation)
163158
}
164159

165160
private func getPinAnnotationView(annotation: MKAnnotation, id: String) -> MKPinAnnotationView {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// AnnotationDelegate.swift
3+
// apple_maps_flutter
4+
//
5+
// Created by Luis Thein on 01.07.21.
6+
//
7+
8+
import Foundation
9+
import MapKit
10+
11+
protocol AnnotationDelegate: AnyObject {
12+
func getAnnotationView(annotation: FlutterAnnotation) -> MKAnnotationView
13+
func annotationsToAdd(annotations :NSArray)
14+
func annotationsToChange(annotations: NSArray)
15+
func annotationsIdsToRemove(annotationIds: NSArray)
16+
func onAnnotationClick(annotation :MKAnnotation)
17+
func showAnnotation(with id: String)
18+
func hideAnnotation(with id: String)
19+
func isAnnotationSelected(with id: String) -> Bool
20+
func removeAllAnnotations()
21+
}

0 commit comments

Comments
 (0)