Hi Folks,
I am building a SwiftUI based WatchOS app primarily focused on updating a complication using data from a REST API call.
I am trying to reproduce the approach to updating complications in the background as described in "Keeping your complications up to date".
The trouble I have is that the func handle(_ backgroundTasks: Set<WKRefreshBackgroundTask>) method on the ExtensionDelegate is never called for the WKURLSessionRefreshBackgroundTask task type. The handle method is invoked for other task types such as WKSnapshotRefreshBackgroundTask.
After some refactoring, I have deviated from the approach in the video and I now reshedule the background task in the func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) method of the URLSessionDownloadDelegate.
This works fine, and the tasks are being resheduled in the background, but my concern is that there maybe a WKURLSessionRefreshBackgroundTask object lurking in the background for which I haven't called setTaskCompletedWithSnapshot.
My session is configured as follows:
private lazy var backgroundURLSession: URLSession = {
let appBundleName = Bundle.main.bundleURL.lastPathComponent.lowercased().replacingOccurrences(of: " ", with: ".")
let sessionIdentifier = "com.gjam.liffeywatch.\(appBundleName)"
let config = URLSessionConfiguration .background(withIdentifier: sessionIdentifier)
config.isDiscretionary = false
config.sessionSendsLaunchEvents = true
config.requestCachePolicy = .reloadIgnoringLocalCacheData
config.urlCache = nil
return URLSession (configuration: config, delegate: self , delegateQueue: nil )
}()
and the scheduling code is a follows:
func schedule(_ first: Bool) {
print("Scheduling background URL session")
let bgTask = backgroundURLSession.downloadTask(with: LiffyDataProvider.url)
#if DEBUG
bgTask.earliestBeginDate = Date().addingTimeInterval(first ? 10 : 60 * 2)
#else
bgTask.earliestBeginDate = Date().addingTimeInterval(first ? 30 : 60 * 60 * 2)
#endif
bgTask.countOfBytesClientExpectsToSend = 200
bgTask.countOfBytesClientExpectsToReceive = 1024
bgTask.resume()
print("Task started")
backgroundTask = bgTask
}
Does anyone have any idea's why the handle method is not invoked with WKURLSessionRefreshBackgroundTask tasks?
Cheers,
Gareth.