Skip to main content
Version: 11.8.0

How to save the progress of a caching task on iOS

The THEOplayer iOS SDK provides the capability to pause an ongoing caching task. Doing so stores the file state of the partially downloaded stream to disk, and allows the task to be resumed even after the application runtime has been terminated. See CachingTask for the pause() method.

Usage

The code example below demonstrates how to save the progress of ongoing caching tasks upon app termination from the app delegate.

import THEOplayerSDK

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func applicationWillTerminate(_ application: UIApplication) {
for task in THEOplayer.cache.tasks {
if task.status == .loading {
task.pause()
}
}
}
}

Alternatively, Apple's NotificationCenter provides an observer to receive app termination events:

import THEOplayerSDK

NotificationCenter.default.addObserver(
self,
selector: #selector(willTerminate),
name: UIApplication.willTerminateNotification,
object: nil
)

@objc func willTerminate() {
for task in THEOplayer.cache.tasks {
if task.status == .loading {
task.pause()
}
}
}

Limitations

The THEOplayer iOS SDK allows the caching task to continue while running in the background. However, control in the background thread is limited, and the termination signal falls short as noted in Apple's documentation.