Minimize Networking
Network operations may be unavoidable and essential to your app. In many cases, however, networking can be minimized by adhering to some general guidelines.
Reduce Data Sizes
Network transactions should be as small as possible to reduce overhead.
Reduce Media Quality and Size
If your app uploads, downloads, or streams media content, lower quality and smaller sizes reduce the amount of data being sent and received. Some apps let the user specify the quality and size. For example, when emailing a photo, Mail lets the user send a scaled version of the image at small, medium, or large size. The smallest size is the most energy efficient.
Compress Data
Use compression algorithms to compact data as much as possible before sending or receiving it.
Avoid Redundant Transfers
Your app shouldn’t repeatedly download the same data.
Cache Data
Use caching to store infrequently updated data locally. Redownload the data only when it has changed or the user requests it. The NSURLCache
and NSURLSession
APIs can be used to implement in-memory and on-disk caches for URL request data.
Use Pausable and Resumable Transactions
Network conditions fluctuate, and signal loss can be a regular occurrence. Be prepared to resume interrupted transactions so the same content isn’t downloaded multiple times. In some cases, it makes sense to let users pause downloads and resume them later. For example, iOS lets users pause app downloads by tapping on a partially downloaded app icon.
The NSURLSession
API lets you implement pause and resume functionality, without implementing caching.
Handle Errors
Don’t attempt to perform network operations when the network is unavailable.
Check Signal Conditions
If network operations fail, use the SCNetworkReachability
API to to see whether the host is available. If there are signal problems, alert the user or defer work until the host is available again.
To determine whether a host is reachable, check for the absence of the kSCNetworkReachabilityFlagsReachable
reachability flag, as demonstrated in Listing 9-1.
Objective-C
#import "SystemConfiguration/SCNetworkReachability.h"
...
// Create a reachability object for the desired host
NSString *hostName = @"someHostName";
SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, [hostName UTF8String]);
// Create a place in memory for reachability flags
SCNetworkReachabilityFlags flags;
// Check the reachability of the host
SCNetworkReachabilityGetFlags(reachability, &flags);
// Release the reachability object
CFRelease(reachability);
// Check to see if the reachable flag is set
if ((flags & kSCNetworkReachabilityFlagsReachable) == 0) {
// The target host is not reachable
// Alert the user or defer the activity
}
Swift
import SystemConfiguration
...
// Create a reachability object for the desired host
let hostName = "someHostName"
let reachability = SCNetworkReachabilityCreateWithName(nil, (hostName as NSString).UTF8String).takeRetainedValue()
// Create a place in memory for reachability flags
var flags: SCNetworkReachabilityFlags = 0
// Check the reachability of the host
SCNetworkReachabilityGetFlags(reachability, &flags)
// Check to see if the reachable flag is set
if ((flags & kSCNetworkReachabilityFlagsReachable) == 0) {
// The target host is not reachable
// Alert the user or defer the activity
}
Provide an Escape Route
Don’t wait forever for a response from the server that never comes. Let the user cancel long-running or stalled network operations, and set appropriate timeouts so your app doesn’t keep connections open needlessly.
Use Retry Policies
If a transaction fails, try again when the network becomes available. Use the SCNetworkReachability
API to determine or be notified when the network is available again.
Copyright © 2018 Apple Inc. All rights reserved. Terms of Use | Privacy Policy | Updated: 2016-09-13