I have developed an application for iPhone that runs in the background with GPS mode on. I need to ask the server if there are any new tasks for the user? Fir this purpose, I have added the code in the didUpdateToLocation method. Now the problem is that it consumes the battery very fast. Please guide me how to avoid the battery consumption. Also, I need to keep the location accuracy at best.
Also, is there any other way where I can communicate to the server even while the app is running in the background. Please help me, I will be really grateful.
It is impossible to get accurate location without using internal GPS in the current state of iPhone. Since essentially you are keeping the GPS alive during the running of the app (not sure if Multitasking API allows GPS calling in background though), it would consume lots of battery power without doubt.
In this case, you either have to avoid calling GPS as much, or live with coarse locations from the cellar towers. You can't have both frequent GPS results and nice battery consumption.
Related
Some of my users ran into an issue where they couldn't fetch the location using Location Service.
In the screenshots they sent me, i noticed that the battery level was really low, like 2-5%.
My question is, does iOS stop the location service when the device is running out of battery?
Thanks
Enabling Low Power Mode (the one that turns the battery icon yellow) does several things to reduce the battery usage of your device. This probably includes the accuracy and refresh rate of the GPS.
If you're requesting a location with a high level of accuracy, the system might either take too long and timeout or just decide not to fulfill that request since the hit on the battery would be too much.
does iOS stop the location service when the device is running out of battery
It certainly might. The GPS is one of the highest power draws of all the sensors. Turning it off would make a lot sense. See also Apple's own statements about low power mode:
https://support.apple.com/en-us/HT205234
Note that if this is the case, it would hardly be an issue with your app. Your app might use a lot of battery, which could be a problem; but if location services itself is affected, all map/navigation apps would be affected.
I am trying to set up an app that will be able to check people's locations in the background, see if they are at a given location, and send a ping to a server if they are. We do not want to drain the energy of our users, so we are attempting to figure out the best solution.
I've done substantial reading and I have not found very much information on these methods. I'll go through the pros and cons as I understand them right now
startMonitoringForSignificantChanges
Description: Based off of wi-fi and cell tower changes the system wakes up the app.
Docs:
Apps can expect a notification as soon as the device moves 500 meters
or more from its previous notification. It should not expect
notifications more frequently than once every five minutes. If the
device is able to retrieve data from the network, the location manager
is much more likely to deliver notifications in a timely manner.
Pros:
Most battery efficient
Cons:
Dependent on wi-fi/cell tower changes
Can only assume that this will be called every 200m to 2km (if not more in certain areas)
More on accuracy
Thus, inconsistent and imprecise
10-minute start-updating or "n-minute updating":
Description: This basically asks the app for more time, when that extra time is about to expire, it calls [self.locationManager startUpdating], grabs the location and extends the background thread for 10 more minutes.
Pros:
Consistent
Can be as accurate as you want it to be as consistently as you
want it
Cons:
Has to do a call every ten minutes or less to keep the app running in the
background (ie n can't be greater than 10 for the calls)
Questions:
What effect does this have on the battery? Does waking up the GPS and shutting it off hurt the battery more? I couldn't imagine running a brief location check in the background would drain the battery that much... but then again, I don't know what goes into powering up the GPS and getting a usable signal.
startMonitoringForRegion (geo-fencing):
Simply put, your app gets woken up when you enter into a pre-defined region. This is the oddball of them, it is more recent and there is less documentation on it. I can't find a good description on how the "system monitors" the boundary crossing. For all I know it is some really smart algorithm, or they are constantly pinging the GPS which would make it less effective than the other methods for doing this.
Pros:
Simple implementation
Managed by the system so you don't have to invent your own ad hoc geo-fences Only triggers on boundary crossing... no unnecessary data to just throw out in exchange for a battery hit
Thus, should be the best for this sort of thing, accurate, managed by the system
Cons:
People question its effectiveness
Huge conflicts on whether or not it is good for battery life or if it
drains battery life terribly.
How is the system monitoring this!?
Basically, indeterminate behavior.
I guess my question boils down to how does startMonitoringForRegion: compare to these other methods of testing user location in the background when it comes to battery life, consistency, and precision. Has anyone thoroughly tested this? Or used it in their app and gotten at least some feedback? Likely, for my purposes, the trade-off is between geo-fencing and the 10 minute update method. (Also given what Apple has publicly said about iOS7 there will be some background tasks... will this change the calculus for the trade-off between these two methods?) Does anyone have an idea of how these two compare?
Thanks so much! Looking forward to seeing if we can get to the bottom of how to compare these methods.
I've been working on vehicle tracking using GPS for 2 years. Learned a lot the hard way... In my experience startMonitoringForRegion or Geo-fencing depends upon cell change events, didEnter or didExit events doesn't fire up until there is a cell/wifi change event. So it doesn't make any difference w.r.t battery consumption. However it does extra computation which depends on how many regions currently are being monitored. Even Apple's Reminder app doesn't give good results for location based reminders because it uses geo-fencing.
The other approach starting GPS for n minutes after each m-minutes is good option, it should not affect the battery life, if done wisely. What exactly effect the battery is constant GPS activation in high precision mode. Like for instance If you enable GPS with kCLLocationAccuracyBest and distance-filter = 0, you can literally observe battery drainage and soon your device will also start getting hotter.
If I was you, I would go for activating GPS after every 10 minutes for 5 sec with kCLLocationAccuracyBest (or may kCLLocationAccuracyNearestTenMeters to use less battery, if accuracy is not that much important) and distance-filter = 5 (meters). Battery consumption in this case will be unnoticeable. You can play around with similar settings which can address your specific case and finally find out what is best you.
BTW: iPhone uses AGPS, A-GPS additionally uses network resources to locate and use the satellites in poor signal conditions. So, when you do startUpdatingLocation it will also use nearby cell tower information. see http://en.wikipedia.org/wiki/Assisted_GPS
I would like you to give me your feedback on which method consumes less battery.
My app will run in the background and will wake up with location changes, so I would like to use the method that consumes less battery.
Any ideas on which one it is?
Thanks
Neither of these choices is responsible for more or less battery consumption.
In order for your app to be notified of any location update, regardless of whether it is for a region change or a significant location change, you must specify in your app's Info.plist file that you will require location-services in the background.
There are actually two relevant choices for location services: location-services or gps.
In order NOT to drain the user's battery, choose location-services. If you say your app needs gps background services, you WILL drain the battery because this will cause the GPS hardware (assuming it is present) to be enabled, and THAT is the cause of battery drain. When you specify location-services, the device uses the cellular radio (which is on anyway, assuming you have an iPhone) to pinpoint the location instead. Not as accurate as gps, but most apps don't need GPS accuracy. (If you do, then use gps, of course, but know the consequences vis a vie battery life.)
I recently wrote a test app all about this (and I wrote about it last week here) and what I found was there was no significant battery drain when I had several regions setup for monitoring and I specified location-services as a required background service.
Question: Does the Significant Location Change background service drain battery easily? I'm trying to track longitude latitude readings when the there is a Significant Location Change. However, before starting, I just want to make sure that this doesn't drain the battery.
If the Location service described above does not drain battery, can someone recommend a way to store the location changes that would be battery efficient. My initial thought was to store the longitude latitude points locally on iPhone and then send the information to the server on a much less frequent basis (2 hours). Anyone have a better approach?
I'm trying to build a simple location tracking app that is battery efficient. Thanks all.
Significant location changes do not take any extra battery draining because the device uses information that the GSM system is working with anyway. It has to keep track of signal strength readings of multiple cell towers all the time anyway. Your app is only started/woken when certain criteria are met.
To conserve battery you have to watch two subsystems:
make sure that CoreLocation is turned off when you don't need it, GPS takes the most
make sure that you send location data to your server in bursts, to allows the transmitter to power down.
2 hrs might be too much because the user might be terminating the app and then the updates would never be made. Or if you implement an offline queue, they would only be sent next time the app is started. But that depends on your specific scenario.
It should be cellular tower triangulation, and not GPS location, which is more than enough for your needs. It shouldn't be battery heavy at all.Same with uploading the location, do it rarely and you are fine.
Edit: Confused Core Location with Significant Location.
I'm currently testing the latest iOS4 Feature to put my location aware app in the background. Well, it does work! But on the other hand it's quite hart to handle the immense power usage.
The app consumed about 50% battery power in the last four hours. It read the entire official documentation by Apple on this topic but I'm still not sure which parts of my application are still running and which functionality is suspended (beside the UI Drawing, which should be clear).
I don't use any real boilerplate code but extended libraries like ASIHTTPRequest to talk to my webservice. Tests with a friends car did you show that the Network Connectivity and and the Location Services is still running when I'm using i.e.
[locationManager startMonitoringSignificantLocationChanges];
Apples Documentation on the different application states
Background: The application is in the background and executing code
[...]
Should I write a "bare metal" functions to receive and send this location data? Should I remove all other objects for the time the application resides in the background to reduce the memory footprint? It seems there isn't any best practice yet.
Any ideas? Maybe you guys can provide me with some of your insights. Thanks.
Edit:
There's a new Instruments tool called Energy Diagnostics Instruments to record any power usage (for iPhone 3GS and later) with an attached device. Also there's another service on the device in the Settings App -> Developer -> Power Usage. It's great to test your power usage in field. The created logs can be pulled later in instruments.
Reference: WWDC 2010 Session 309 - Advanced Performance Analysis with Instruments
Sounds like your app is transmitting location data over the cellular network. Turning on the cellular radio is one of the most rapid causes of power drain, especially if the user has a weak signal connection to the cell tower.
You might want to save and package up a bunch of location data, and send the data in a quick burst as seldom as possible (twice per day, when the user stops moving for 30 minutes, only after the user gets to one of their favorite restaurants, etc.) Turning on the radio less than half as often could get you close to doubling the battery life (unless the user is doing something else with the device as well).