Background data task in watchOS

Viewed 268

I'm trying to build a basic proof-of-concept watchOS app and complication that pulls JSON data from an API and displays a gauge based on that. I've watched Apple's 'Keping your watch app up to date' and found several other questions on the subject but the sample code has been taken down.

The API provides forecasts for the next ~48 hours and can be used to populate the timeline entries for the complication. When the Complication Controller requests the timeline entries I pull the data from the Extension Delegate and therefore it must be kept up to date. However the process of scheduling background data tasks has got me stumped. When I call backgroundSession.dataTask(with: URL(string: "https://...... I expect my URLSessionDataDelegate functions to be called but they never are and I never get a the handle(_ backgroundTasks) called with WKURLSessionRefreshBackgroundTask

Question: When a WKApplicationRefreshBackgroundTask is sent to my ExtensionDelegate how should I request/schedule data from the API and then receive it?

Code

//ExtensionDelegate
class ExtensionDelegate: NSObject, WKExtensionDelegate, URLSessionDelegate, URLSessionDataDelegate {

...


func handle(_ backgroundTasks: Set<WKRefreshBackgroundTask>) {
    for task in backgroundTasks {
        switch task {
        case let backgroundTask as WKApplicationRefreshBackgroundTask:
            print("background task as WKApplicationRefreshBackgroundTask")
            self.scheduleURLSession()
            backgroundTask.setTaskCompletedWithSnapshot(false)
            return


        case let urlSessionTask as WKURLSessionRefreshBackgroundTask:
            print("background task as WKURLSessionRefreshBackgroundTask")
            let backgroundConfigObject =
                URLSessionConfiguration.background(withIdentifier: urlSessionTask.sessionIdentifier)
            let backgroundSession = URLSession(configuration: backgroundConfigObject, delegate: self, delegateQueue: nil)
            print("Rejoining session ", backgroundSession)

            self.savedTask = urlSessionTask
            return
        default:
            // make sure to complete unhandled task types
            task.setTaskCompletedWithSnapshot(false)
        }
    }
}





func scheduleURLSession() {
    let backgroundConfigObject = URLSessionConfiguration.background(withIdentifier: "nz.co.craigstanton")
    backgroundConfigObject.sessionSendsLaunchEvents = true

    let backgroundSession = URLSession(configuration: backgroundConfigObject, delegate: self, delegateQueue: nil)

    let dataTask = backgroundSession.dataTask(with: URL(string: "https://craigstanton.co.nz/uvi-test?latitude=-36&longitude=174")!)

    print("scheduleURLSession about to 'resume' ")
    dataTask.resume()
}


//Delegate callbacks
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
    print("Data task error", error)
}


func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse,
                    completionHandler: @escaping (URLSession.ResponseDisposition) -> Void) {

    print("urlSession Delegate did receive everything ")
}

func urlSession(_: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
    print("urlSession Delegate did receive something ")
}
0 Answers
Related