13

I have a pretty simple app, with a couple of view controllers. There is a MKMapView in the second view controller. It is set up correctly, and functions fine. The problem is, each time I load its view the Memory usage jumps ~30mb, and never goes back down, so each time i go into the view it keeps jumping and eventually gets super high. I tried removing the map view when i leave the controller like this:

override func viewWillDisappear(animated: Bool) {
        map.removeFromSuperview()
    }

but it doesn't have any effect on the memory. The map views delegate is set to its view controller.

I tried checking for leaks using Xcode instruments but didn't find anything.

Does anyone know how to fix this?

Thanks

EDIT: Adding this seems to work:

func removeNastyMapMemory() {
        map.mapType = MKMapType.Hybrid
        map.delegate = nil
        map.removeFromSuperview()
        map = nil
    }

    override func viewWillDisappear(animated: Bool) {
        removeNastyMapMemory()
    }
Bullwinkle
  • 350
  • 4
  • 19

1 Answers1

8

This is not Swift issue, is coming from Objective-C days. The possible ways to handle this issue is depending upon the situation and behavior of the app.

  1. If you're using a Map for multiple times (or places), only create a single (shared) instance of it. Which you can use it whenever you want.

  2. Or If you're only using it for once, then try a solution from here, https://stackoverflow.com/a/25419783/1603234. This may help. Reduce little. But not all.

Community
  • 1
  • 1
Hemang
  • 26,840
  • 19
  • 119
  • 186
  • I tried 2 and seems to work. (i'll post above). Not really sure how to go about 1, which i think would be better, I'm pretty new to iOS and map views. – Bullwinkle Apr 05 '16 at 05:06