简体   繁体   中英

iOS Swift: Google Maps and MapKit access doubles memory usage

My goal is to: Smartly manage user phone memory and battery use when the user is using my app.

I am working with MapKit and Google Maps in my iOS App. Initially when the app is launched, I am asking for authorisation to access the location (which I am using later in the app).

First place where I am using map is while getting a route. Authorisation code is as below:

func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
    if status == CLAuthorizationStatus.AuthorizedWhenInUse {
        viewMap.myLocationEnabled = true
    } else if status == CLAuthorizationStatus.NotDetermined {
        println("Location disabled")
        viewMap.myLocationEnabled = false
    }
}

Second place where I am using the map is to retrieve some data created at particular location during my first map access. Basically just the current location as below.

locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()
        startLocation = nil

once I have the location, I call this:

    locationManager.stopUpdatingLocation()

The question is: As soon as the app launches, I can see the sign (small triangle in the status bar) stating that the GPS is active and accessing the location. It stays on throughout the time my app is active (even when it is in background). Until I forcefully exit the app (multitasking menu slide up to exit), the GPS access sign stays on. Once the user access the map, the memory usage doubles (40Mb to 100mb) and keeps increasing while user scrolls the Google map view. Even when I exit t he map, the memory stays at 100+ mbs.

What I want to know is, - Am I using the maps properly here? - What else can I do so that I don't use the GPS unless I really need it? Is there a function I can call after exiting the viewControllers which are accessing the map? - Anything else that would be helpful?

Thanks.

What you are doing is OK - If you are calling stopUpdatingLocation when appropriate then you are minimising the impact of location services on the device. As far as I know, the location service arrow in the status bar doesn't indicate the status of the GPS receiver or any other location services hardware, it merely indicates to the user that your app is using location services. Even if your app isn't currently receiving location updates, it has accessed the user location and has permission to do so, so the user is notified that their location is being used.

You should, however, be wary of turning off location services updates as soon as you receive a location as the initial location fix may be inaccurate - you should check the horizontalAccuracy property to decide if the location is accurate enough.

As far as memory usage goes, the map views will be caching map imagery that is off-screen. Fetching this imagery consumes data (possibly cellular data) and battery to run the radios; it is far better for your app to consume memory than to needlessly fetch the same data repeatedly.

Remember that iOS isn't a multi-tasking environment in the way that Windows or OS X is - other apps aren't really running when your app is and when your app is in the background it can be purged if memory is short (you can also receive low memory notifications when your app is in the foreground).

到目前为止,还没有好的解决方案,但是使用iOS9,您将能够在短时间内访问该位置以更新用户的位置。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM