Schedule Background Networking
Apps that perform background networking should use appropriate APIs to limit impact on the system and increase energy efficiency. Nonessential network activity should allow the system to schedule the network activity for optimal times, such as when the Mac is plugged-in or on Wi-Fi.
Schedule Deferrable Network Operations
For background upload and download activities over HTTP, NSURLSession
provides the ability to create background sessions. A background session allows your app to issue URL requests to be performed. These requests are then sent to a system daemon, which performs them out-of-process and notifies your app when they are complete. There are numerous advantages to using this method for background network activity:
Activity is performed out-of-process. Because the network activity is performed by the system, your app doesn’t need to be running anymore for activity to complete.
Notifications keep your app informed. The system notifies your delegate when the activity is completed. Your app can even quit, relaunch, reconnect to a previous session, and resume receiving callbacks. If your app isn’t running when the activities complete, or if authentication is required, the system can even launch your app in the background.
Network activity is performed efficiently. It’s inefficient to perform network activity over a slow connection. Bandwidth monitoring allows activity to be automatically deferred when throughput falls below a certain threshold. A background session can also defer activity when system thermals rise.
Activity self-corrects. URL sessions can be automatically retried when errors occur.
Configure a Background Session
To create a background session, create a new NSURLSession
, give it a unique session identifier, and denote it as discretionary activity, as shown in Listing 13-1. Once the session has been created, you can use the unique identifier to reconnect to the session if your app quits and relaunches.
Objective-C
// Set up a configuration with a background session ID
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration
backgroundSessionConfigurationWithIdentifier:@"com.<YourApp>.<YourBackgroundSessionIdentifier>"];
// Set configuration to discretionary
configuration.discretionary = YES;
// Create URL session
NSURLSession *backgroundSession = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
Swift
// Set up a configuration with a background session ID
var configuration = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("com.<YourApp>.<YourBackgroundSessionIdentifier>")
// Set configuration to discretionary
configuration.discretionary = true
// Create URL session
var backgroundSession = NSURLSession(configuration: configuration, delegate: self, delegateQueue: nil)
Next, create an instance of NSURLRequest
, and pass it to the background session, as shown in Listing 13-2.
Objective-C
// Set up a URL
NSURL *someURLToDownload = [NSURL URLWithString:<YourURLString>];
// Create a URL request
NSURLRequest *downloadRequest = [NSURLRequest requestWithURL:someURLToDownload];
// Add the request to the background session
NSURLSessionDownloadTask *downloadTask = [backgroundSession downloadTaskWithRequest:downloadRequest];
// Initiate the activity
[downloadTask resume];
Swift
// Set up a URL
var someURLToDownload = NSURL.URLWithString(<YourURLString>)
// Create a URL request
var downloadRequest = NSURLRequest.requestWithURL(someURLToDownload)
// Add request to background session
var downloadTask = backgroundSession.downloadTaskWithRequest(downloadRequest)
// Initiate activity
downloadTask.resume()
To receive callbacks when the activity completes, implement the desired delegate methods, as demonstrated in Listing 13-3.
Objective-C
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {
// Do any work following the completed download
};
Swift
func URLSession(session: NSURLSession, downloadTask downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL) {
// Do any work following completed download
}
For additional information on the available delegate methods, refer to the relevant documentation:
For detailed information about creating URL sessions and requests, see NSURL Class Reference, NSURLRequest Class Reference, and NSURLSession Class Reference.
Copyright © 2018 Apple Inc. All rights reserved. Terms of Use | Privacy Policy | Updated: 2016-09-13