The URLSessionDownloadTask never starts the handle (_ backgroundTasks: Set <WKRefreshBackgroundTask>) for the WKURLSession in the background.
Trying to understand why while scheduling a background URLSessionDownloadTask in WatchKit 3.0. func handle(_ backgroundTasks: Set<WKRefreshBackgroundTask>)
never works for WKURLSessionRefreshBackgroundTask
, but other tasks are executed as WKApplicationRefreshBackgroundTask
and WKSnapshotRefreshBackgroundTask
.
My code is in hours ExtensionDelegate
...
func applicationDidBecomeActive() {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
scheduleBackgroundRefresh(in: 10)
}
func handle(_ backgroundTasks: Set<WKRefreshBackgroundTask>) {
// Sent when the system needs to launch the application in the background to process tasks. Tasks arrive in a set, so loop through and process each one.
for task in backgroundTasks {
// Use a switch statement to check the task type
switch task {
case let backgroundTask as WKApplicationRefreshBackgroundTask:
// Be sure to complete the background task once you’re done.
scheduleURLSession()
backgroundTask.setTaskCompleted()
case let snapshotTask as WKSnapshotRefreshBackgroundTask:
// Snapshot tasks have a unique completion call, make sure to set your expiration date
snapshotTask.setTaskCompleted(restoredDefaultState: true, estimatedSnapshotExpiration: Date.distantFuture, userInfo: nil)
case let connectivityTask as WKWatchConnectivityRefreshBackgroundTask:
// Be sure to complete the connectivity task once you’re done.
connectivityTask.setTaskCompleted()
case let urlSessionTask as WKURLSessionRefreshBackgroundTask: // This is never fired, Don't know why, arrrgggggg
// Be sure to complete the URL session task once you’re done.
let backgroundConfigObject = URLSessionConfiguration.background(withIdentifier: urlSessionTask.sessionIdentifier)
let backgroundSession = URLSession(configuration: backgroundConfigObject, delegate: self, delegateQueue: nil) // delegate set to self here after rejoining the session
print("Rejoining session ", backgroundSession)
urlSessionTask.setTaskCompleted() // probably need to postpone this until the url `urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL)` has completed, but either way, this switch case is never met
default:
// make sure to complete unhandled task types
task.setTaskCompleted()
}
}
}
func scheduleURLSession() {
if let url = URL(string: "https://some.path.com") {
let backgroundConfigObject = URLSessionConfiguration.background(withIdentifier: NSUUID().uuidString)
let backgroundSession = URLSession(configuration: backgroundConfigObject, delegate: nil, delegateQueue: nil) // nil delegate here because delegate is "supposed" to be set in `handle(_ backgroundTasks:)` delegate method ("Rejoining session").
let task = backgroundSession.downloadTask(with: url)
task.resume()
} else {
print("Url error")
}
}
func scheduleBackgroundRefresh(in seconds: TimeInterval) {
let fireDate = Date(timeIntervalSinceNow: seconds)
// optional, any SecureCoding compliant data can be passed here
let userInfo = ["reason" : "background update"] as NSDictionary
WKExtension.shared().scheduleBackgroundRefresh(withPreferredDate: fireDate, userInfo: userInfo) { (error) in
if (error == nil) {
print("successfully scheduled background task, use the crown to send the app to the background and wait for handle:BackgroundTasks to fire.")
}
}
}
Here's an Apple sample code example for reference
Any ideas why URLSessionDownloadTask
the background never launches handle(_ backgroundTasks: Set<WKRefreshBackgroundTask>)
to run?
source to share
No one has answered this question yet
Check out similar questions: