Академический Документы
Профессиональный Документы
Культура Документы
Session 238
© 2017 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple.
•
Final Thoughts
General Battery Life Concepts
•
What Is Energy?
Power
Time
What Is Energy?
Active
Active
Power
Overhead Overhead
Idle
Time
What Is Energy?
Active
Active
Power
Overhead Overhead
Energy
Idle
Time
What Is Energy?
Active
Your work
Overhead
Active
Power
Fixed cost
Idle
Time
Balancing Power and Battery Life
Balancing Power and Battery Life
What Consumes Energy?
What Consumes Energy?
Identify
Optimize
Coalesce
Reduce
Energy Efficient Coding
•
Social Networking App
Main feed
Post a photo
Analytics
Social Networking App
Main Feed
Current implementation
• Reloads on a timer
Social Networking App
Energy Impact
Feed Refresh
Power
Time
Social Networking App
Energy Impact
Feed Refresh
Power
Time
Social Networking App
Energy Impact
Feed Refresh
Radio cost
Power
Dynamic cost
Overhead cost
Time
Social Networking App
Main Feed
• Notification
• Cache
Social Networking App
Energy Impact
Feed Refresh
Radio cost
Power
Dynamic cost
Overhead cost
Time
// Setup NSURLSession Default Session
let config = URLSessionConfiguration.default()
// Use WaitsForConnectivity
config.waitsForConnectivity = true
// NSURLSession Cache
let cachesDirectoryURL = FileManager.default().urlsForDirectory(.cachesDirectory,
inDomains: .userDomainMask).first!
let cacheURL = try! cachesDirectoryURL.appendingPathComponent("MyCache")
var diskPath = cacheURL.path
// Use WaitsForConnectivity
config.waitsForConnectivity = true
// NSURLSession Cache
let cachesDirectoryURL = FileManager.default().urlsForDirectory(.cachesDirectory,
inDomains: .userDomainMask).first!
let cacheURL = try! cachesDirectoryURL.appendingPathComponent("MyCache")
var diskPath = cacheURL.path
// Use WaitsForConnectivity
config.waitsForConnectivity = true
// NSURLSession Cache
let cachesDirectoryURL = FileManager.default().urlsForDirectory(.cachesDirectory,
inDomains: .userDomainMask).first!
let cacheURL = try! cachesDirectoryURL.appendingPathComponent("MyCache")
var diskPath = cacheURL.path
// Use WaitsForConnectivity
config.waitsForConnectivity = true
// NSURLSession Cache
let cachesDirectoryURL = FileManager.default().urlsForDirectory(.cachesDirectory,
inDomains: .userDomainMask).first!
let cacheURL = try! cachesDirectoryURL.appendingPathComponent("MyCache")
var diskPath = cacheURL.path
Current implementation
• Send immediately
• Retry on failure
Social Networking App
Energy Impact
Dynamic cost
Overhead cost
Time
Social Networking App
Energy Impact
Dynamic cost
Overhead cost
Time
Social Networking App
Posting a Photo
• Set timeouts
• Batch Transactions
Dynamic cost
Overhead cost
Time
Social Networking App
Sending analytics data
Properties
• New
- Start time
- Workload size
• Discretionary
Social Networking App
Sending analytics data
Properties
• New
- Start time
- Workload size
• Discretionary
Social Networking Application
Energy Impact
Dynamic cost
Overhead cost
Time
Social Networking Application
Energy Impact
Dynamic cost
Overhead cost
Time
// Setup NSURLSession Background Session
let config = URLSessionConfiguration.background(withIdentifier: "com.socialapp.background")
let session = URLSession(configuration: config, delegate: …, delegateQueue: …)
task.resume()
// Setup NSURLSession Background Session
let config = URLSessionConfiguration.background(withIdentifier: "com.socialapp.background")
let session = URLSession(configuration: config, delegate: …, delegateQueue: …)
task.resume()
// Setup NSURLSession Background Session
let config = URLSessionConfiguration.background(withIdentifier: "com.socialapp.background")
let session = URLSession(configuration: config, delegate: …, delegateQueue: …)
task.resume()
Social Networking Application
WatchOS
Identify
• Ensure transactions not repeated
Optimize
• Use background session
Coalesce
• Batch transactions
Reduce
• Minimize retries
Location Best Practices
Location APIs
Continuous location
Region monitoring
Visit monitoring
Navigate to a destination
• Continuous location updates
locationManager.requestLocation()
Location Best Practices
Request location
locationManager.requestLocation()
Location Best Practices
Region monitoring
// Start monitoring
locationManager.startMonitoringVisits()
// Start monitoring
locationManager.startMonitoringVisits()
// Start monitoring
locationManager.startMonitoringVisits()
// Start monitoring
locationManager.startMonitoringSignificantLocationChanges()
// Start monitoring
locationManager.startMonitoringSignificantLocationChanges()
// Start monitoring
locationManager.startMonitoringSignificantLocationChanges()
Identify:
• Accuracy level needed
Optimize:
• Use alternatives to continuous location
Reduce:
• Stop location when not used
Coalesce:
• Defer location updates
Graphics
MTLCreateSystemDefaultDevice()
• Always uses Discrete GPU
Identify:
• Blur usage
Optimize:
• Only use discrete GPU when needed (macOS)
Reduce:
• Minimize screen updates
Processing Best Practices
Identify tasks
Avoid timers
Set leeway
Background Processing
Foregrounded
Dynamic cost
Fixed cost
Idle
Time
Background Processing
PushKit API
• Now has completion handler
PushKit API
• Now has completion handler
Background work
Overhead Incoming
Push
Power
Suspended
Dynamic cost
Fixed cost
Idle
Time
Background Processing
With Completion Handler
Background work
Overhead Incoming
Push Completion
Handler
Power
Dynamic cost
Fixed cost
Idle
Time
Background Processing
WatchOS
• Minimize networking
Identify:
• Work done in the background
Optimize:
• Use Background App Refresh
Reduce:
• Limit transactions
Coalesce:
• Use NSURLSession background session
•
Final Thoughts
Overview
Demo
Settings: Battery
Settings: Battery
Energy Gauges
Scenarios for Energy Debugging
General/common
• Launch and Idle
• Background
Application specific
• For example, Navigation App
Final Thoughts
Final Thoughts
Avoid timers
Coalesce work