Download Data in Background URLSession in watchOS 7

136 views Asked by At

I am trying to follow the WWDC20 session "Keep your complications up to date", WWDC20-10049. In trying to follow with the code, I have gotten stuck on the schedule function:

func schedule(_ first: Bool) {

        if backgroundTask == nil { //Issue is here

            if let url = self.currentWeatherURLForLocation(delegate.currentLocationCoordinate)
            {
                let bgTask = backgroundURLSession.downloadTask(with: url)

                bgTask.earliestBeginDate = Date().addingTimeInterval(first ? 60 : 15*60)

                bgTask.countOfBytesClientExpectsToSend = 200
                bgTask.countOfBytesClientExpectsToReceive = 1024

                bgTask.resume()

                backgroundTask = bgTask //Issue is here
            }
        }
    }
}

The issue I am having, since the session only provided snippets, is where the var backgroundTask is defined. This function is declared in the data model, in this case WeatherDataProvider. It is obviously a backgroundURLSession.downloadTask, but it is not identified as self so it should be local to the function, but it is not shown in the snippet. I would expect it to be either sent in as a parameter to the function, or declared in the data model itself. If it is declared there, how is it declared as, according to the talk, it has access to prior background requests as the snippet is checkin to see if there is a background request in the queue so the function doesn't schedule another one. I have searched for the last couple of days, but can find nothing that implements this that I can use to understand where Apple is coming from in this session.

I have checked out this answer, as well as this overview of the session, but I haven't figured this out.

Thanks.

0

There are 0 answers