1. Network optimization

  • Check the network status before making a request. If the network is unavailable, do not attempt to make a network request.
  • Set a proper timeout period before the request to avoid network operations that run for a long time or are slow.
  • Try to use breakpoint continuation during the request process, otherwise the same content may be transmitted many times when the network is unstable.
  • Reduce and compress network data;
  • Request results using caching;
  • Network activity arouses radio modules that require long periods of periodic power supply, allowing network requests to be made in batches to reduce overhead;

2. Positioning optimization

  • Precision & high frequency positioning will increase overhead, need to use on demand;
  • If it is not a navigation application, try not to update the location in real time, turn off the location service after positioning;
  • If you just need to quickly determine the user’s location, you are best to use the requestLocation method of CLLocationManager. After the positioning is completed, the positioning hardware will be automatically powered off.
  • Try to reduce the positioning accuracy, such as try not to use the highest accuracy KCLLocationAccuracyBest. The higher the precision, the greater the power consumption of the hardware module;
  • Need background position, try to set up pauseLocationUpdatesAutomatically to YES, if the user is unlikely to move the system will automatically suspend update;
  • Try not to use startMonitoringSignificantLocationChanges, preferred startMonitoringForRegion;

3. CPU optimization

  • The time interval of Timer should not be too short to meet the requirements;
  • Appropriate number of threads, not too many, do not block the main thread;
  • Optimization algorithm, reduce the number of cycles;
  • Image processing:

The image is the same size as the imageView. To avoid unnecessary processing, you can use the entire image, increasing the application size, but saving CPU

  • Cache of complex computations (row height of UITableView)
  • Lazy loading, do not create all subViews at once, but as needed;
  • Object reuse to avoid repeated creation;
  • Avoid big XIBs, storyboards, and use pure code;
  • Reduce the use of Autolayout in complex views
  • Do not refresh the page frequently. If you can refresh one cell line, you should refresh only one cell line and try not to use reloadData.
  • Select the right set:

NSArray, very fast to find using index, very slow to insert and delete, NSDictionary dictionary, very fast to find using keys, NSSets, unordered, very fast to find using keys, very fast to insert/delete

  • Use expensive objects like NSDateFormatter and NSCalendar wisely. Performance tests show that the performance bottleneck of NSDateFormatter is due to the conversion of NSDate format to NSString format, so creating a singleton for NSDateFormatter doesn’t make sense. The recommended practice is to cache the most commonly used date formats.
static NSDateFormatter *cachedDateFormatter = nil;
+ (NSDateFormatter *)cachedDateFormatter {
if(! dateFormatter) { dateFormatter = [[NSDateFormatter alloc] init]; [dateFormattersetThe DateFormat: @ "MM - dd YYYY - HH: MM: ss"); }return dateFormatter;
}
Copy the code

4. GPU optimization

  • Indiscriminate use of gpus leads to poor interaction and reduced battery life;
  • Use less operations to get rounded corners, whether view.maskToBounds or layer.clipToBounds will have a lot of resource overhead, must use rounded corners, it is better to make the image itself rounded corners;
  • Use transparency or semitransparent as little as possible, which will generate additional calculations;

5. Background optimization

  • Background state apps still consume power. They need to perform background operations on demand and use latency APIs to ensure efficient execution of system operations.
  • When the APP enters the background state, it immediately reduces the actions and notifies the system that these actions have been completed.

6. I/O operations

  • Try not to write small data frequently. If large data is written at one time, use databases (such as Sqlite and Realm) or Dispatch_io to read large amounts of important data.
  • If images need to be read and written, a method similar to the design of SDWebImage framework can be adopted: When images are read and cached, the NSCache of the system is prefered to be used. If the images cannot be found, the disk cache images can be read through I/O, which reduces power consumption.

7. Hardware detection optimization

  • When the user moves, shakes or tilts the device, motion events are generated, which are detected by hardware such as accelerometers, gyroscopes and magnetometers. These hardware should be turned off when testing is not required.