0

I have a tab based application. One of the tabs contains an MKMapview.

When I first launch the app, my memory is around 44 mb.

When I switch to the tab with the map, it jumps right up to 84 mb.

Furthermore, I have added two buttons for zooming in and out as requested by the client.

After just two clicks of the zoomIn button, it jumps to 104 and then 2 clicks of the zoomOut it jumps to 112.

I want to nil out my map view in viewDidDissappear like so to free up some memory:

override func viewDidDisappear(animated: Bool) {
        super.viewDidDisappear(animated)
        self.mapView = nil
}

Then I would need to change my setUpMaps func to be called on viewDidAppear instead of viewDidLoad. It is as follows:

 func setUpMaps(){
        locManager = CLLocationManager()
        locManager.delegate = self
        locManager.startUpdatingLocation()
        self.mapView.delegate = self
        self.mapView.pitchEnabled = true
        self.mapView.showsBuildings = true

        let latitude:CLLocationDegrees = Prefs.latitude
        let longitude: CLLocationDegrees = Prefs.longitude
        let theRegion:MKCoordinateRegion = MKCoordinateRegionMake(CLLocationCoordinate2DMake(latitude,longitude), MKCoordinateSpanMake(0.9, 0.9))
        self.mapView.setRegion(theRegion, animated: true)
        let thePoint = MKPointAnnotation()
        thePoint.coordinate = CLLocationCoordinate2DMake(Prefs.latitude as CLLocationDegrees, Prefs.longitude as CLLocationDegrees)
        thePoint.title = "Your registered location"
        self.mapView.addAnnotation(thePoint)
        addRadiusCircle(CLLocation(latitude: Prefs.latitude as CLLocationDegrees, longitude: Prefs.longitude as CLLocationDegrees), radiusParam: CLLocationDistance(Double(Prefs.radius)))
    }

Obviously this crashes the second time as mapView is nil. So I need to give it a value again. Something like :

var mapView = MKMapView()

But I have mapView defined as an outlet:

@IBOutlet weak var mapView: MKMapView!

Is there any way to reassociate the var mapView with my outlet in the viewDidAppear func? Or should I just get rid of the outlet altogether and create the mapView programmatically? If defining programatically should I be usinig weak var?

zoom funcs:

@IBAction func zoomIn(sender: UIButton) {
    let span = MKCoordinateSpan(latitudeDelta: mapView.region.span.latitudeDelta/2, longitudeDelta: mapView.region.span.longitudeDelta/2)
    mapView.setRegion(MKCoordinateRegion(center: mapView.region.center, span: span), animated: true)
}

@IBAction func zoomOut(sender: UIButton) {
    let span = MKCoordinateSpan(latitudeDelta: mapView.region.span.latitudeDelta*2, longitudeDelta: mapView.region.span.longitudeDelta*2)
    mapView.setRegion(MKCoordinateRegion(center: mapView.region.center, span: span), animated: true)
}
user2363025
  • 6,365
  • 19
  • 48
  • 89
  • Can you show how you did the zoom? Interested about the big memory increase. And have you tried just to remove all notations when disappear? – zc246 Jan 12 '16 at 11:22
  • @zcui93 I added in my func for the zoom in and out. But it's worth noting, that when I pinch and zoom on the map itself, (not using my buttons), I see the same increase! Its actually a bit worse since when doing a pinch and zoom you can do tgreater zooms in one go as opposed to tapping a button that only zooms in/out a certain amount each time – user2363025 Jan 12 '16 at 11:32
  • @user2363025 You can easily add the map as subview, instead of putting the Map in XIB keep and MapContainer of type UIView and add the Map on that, and on disappearing removeFromSuperview. Are you checking this behaviour in Device or, Simulator? – iphonic Jan 12 '16 at 11:34
  • @iphonic add the map as a subview programatically on viewDidAppear you mean, is it? Should it be defined as weak var? I am checking on a device iPhone 6S – user2363025 Jan 12 '16 at 11:37
  • @user2363025 Yes, add it on viewDidAppear.. – iphonic Jan 12 '16 at 11:38
  • Just for reference, map view handles memory warning quite well, mentioned [here](http://stackoverflow.com/a/14198121/2710486). Maybe also worth to consider the singleton. – zc246 Jan 12 '16 at 11:39

0 Answers0