Contents

BADownloadManager

An object that manages the queue of scheduled asset downloads.

Declaration

class BADownloadManager

Overview

Use BADownloadManager to schedule and cancel asset downloads, monitor their progress, and access the queue of pending downloads. You don’t create instances of this class directly; instead, use the shared property to access the framework’s singleton that it shares between your app and the app’s extension. Because the download manager is a shared resource, prevent race conditions by using the withExclusiveControl(_:) and withExclusiveControl(beforeDate:perform:) methods to assume absolute control of the manager before you schedule asset downloads or manipulate those already in the manager’s queue. To respond to asset download events and process concluded downloads, create a type that conforms to the BADownloadManagerDelegate protocol and assign an instance of it to the download manager’s delegate property.

The following example shows how to create an asset download, acquire exclusive control of the shared download manager, and then use the manager to schedule the download:

let url = URL(string: "https://cdn.example.com/level-resources.zip")!
let request = URLRequest(url: url)
let identifier = "group.com.example.my-game"

// Create an asset download.
let download = BAURLDownload(identifier: "level-resources",
                             request: request,
                             applicationGroupIdentifier: identifier)

// Access the shared download manager.
let manager = BADownloadManager.shared

// Assign the manager's delegate so the framework can notify
// the app of asset download events.
manager.delegate = self

do {
    // Attempt to acquire exclusive control of the manager.
    manager.withExclusiveControl { error in
        // Return immediately if that attempt fails.
        if let error {
            print(error.localizedDescription)
            return
        }
        
        // Use the manager to schedule the asset download.
        try manager.schedule(download)
    }
} catch {
    // Handle the error.
    print(error.localizedDescription)
}

Topics

Accessing the download manager

Managing downloads

Monitoring downloads

Fetching in-progress downloads

Synchronizing manager access

See Also

Unmanaged asset downloads