Home > front end >  How to Share 1 MapView in all Project Swift 5
How to Share 1 MapView in all Project Swift 5

Time:02-21

I wanted to know if it was possible to share the same MKMapView variable in the whole project. Since creating multiple instances is increasing memory considerably. As soon as I open a new view controller with just a MapView, it adds 50Mb to memory. And when I dismiss that view controller it only drops about 10Mb.

It is possible to free all the memory of the mapview?. Or is it better to create a single MapView variable and be doing removeAnnotations to create annotations... every time change the map view

var mapview = MKMapView()

When dismiss viewcontroller I do this: (I don't know if anything else is needed.)

mapView.removeAnnotations(AllAnnotations)
mapView.delegate = nil
mapView.removeFromSuperview()

CodePudding user response:

Yes creating many items causes memory gets high even if deinit is called , so you make 1 global map

import MapKit 
let map = MKMapView(frame:CGRect.zero)

and when you need to show it in a vc , add it to view with constraints and after leaving remove it

  • Related