This article is written by Cecil Costa, the author of the book, Swift Cookbook. We'll delve into what profiling is and how we can profile an app by following some simple steps.
It's very common to hear about issues, but if an app doesn't have any important issue, it doesn't mean that it is working fine. Imagine that you have a program that has a memory leak, presumably you won't find any problem using it for 10 minutes. However, a user may find it after using it for a few days. Don't think that this sort of thing is impossible; remember that iOS apps don't terminate, so if you do have memory leaks, it will be kept until your app blows up.
Performance is another important, common topic. What if your app looks okay, but it gets slower with the passing of time? We, therefore, have to be aware of this problem. This kind of test is called profiling and Xcode comes with a very good tool for realizing this operation, which is called Instruments.
In this instance, we will profile our app to visualize the amount of energy wasted by our app and, of course, let's try to reduce it.
(For more resources related to this topic, see here.)
For this recipe you will need a physical device, and to install your app into the device you will need to be enrolled on the Apple Developer Program. If you have both the requirements, the next thing you have to do is create a new project called Chapter 7 Energy.
To profile an app, follow these steps:
@IBOutlet var label: UILabel! @IBOutlet var map: MKMapView!
import CoreLocation import MapKit
override func viewDidLoad() { super.viewDidLoad() locationManager.delegate = self locationManager.desiredAccuracy =
kCLLocationAccuracyBest locationManager.requestWhenInUseAuthorization() locationManager.startUpdatingLocation() }
class ViewController: UIViewController,
CLLocationManagerDelegate { var locationManager = CLLocationManager() var geocoder = CLGeocoder()
func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status:
CLAuthorizationStatus) { var locationStatus:String switch status { case CLAuthorizationStatus.Restricted: locationStatus = "Access: Restricted" break case CLAuthorizationStatus.Denied: locationStatus = "Access: Denied" break case CLAuthorizationStatus.NotDetermined: locationStatus = "Access: NotDetermined" break default: locationStatus = "Access: Allowed" } NSLog(locationStatus) }
func locationManager(manager:CLLocationManager,
didUpdateLocations locations:[AnyObject]) { if locations[0] is CLLocation { let location:CLLocation = locations[0] as
CLLocation self.map.setRegion(
MKCoordinateRegionMakeWithDistance(
location.coordinate, 800,800),
animated: true) geocoder.reverseGeocodeLocation(location,
completionHandler: { (addresses,
error) -> Void in let placeMarket:CLPlacemark =
addresses[0] as CLPlacemark let curraddress:String = (placeMarket.
addressDictionary["FormattedAddressLines"
] as [String]) [0] as String self.label.text = "You are at
(curraddress)" }) } }
The next thing that will happen is that instruments will be opened; probably, a dialog will pop up asking for an administrator account. This is due to the fact that instruments need to use some special permission to access some low-level information.
Now, you can analyze who is spending more energy on you app. To get a better idea of this, go to your code and replace the constant kCLLocationAccuracyBest with kCLLocationAccuracyThreeKilometers and check whether you have saved some energy.
Instruments are a tool used for profiling your application. They give you information about your app which can't be retrieved by code, or at least can't be retrieved easily. You can check whether your app has memory leaks, whether it is loosing performance, and as you can see, whether it is wasting lots of energy or not.
In this recipe we used the GPS because it is a sensor that requires some energy. Also, you can check on the table at the bottom of your instrument to see that Internet requests were completed, which is something that if you do very frequently will also empty your battery fast.
Something you might be asking is: why did we have to change info.plist? Since iOS 8, some sensors require user permission; the GPS is one of them, so you need to report what is the message that will be shown to the user.
I recommend you to read the way instruments work, mainly those that you will use. Check the Apple documentation about instruments to get more details about this (https://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/InstrumentsUserGuide/Introduction/Introduction.html).
In this article, we looked at all the hows and whats of profiling an app. We specifically looked at profiling our app to visualize the amount of energy wasted by our app. So, go ahead to try doing it.
Further resources on this subject: