From the course: Apple watchOS App Development: Advanced APIs

Refresh app in the background

From the course: Apple watchOS App Development: Advanced APIs

Start my 1-month free trial

Refresh app in the background

- [Instructor] With the changes from the challenge, the background running app has a static image in the dock. While we may not want it updating all the time, it may be nice for it to update regularly so we have some sense of the time. This would be a good example of programatically scheduling events. We can schedule background events to do this kind of updating. I'm not going to update the user interface yet, but we'll get the count from the background and print it to the console. You schedule background tasks to the shared extension, which is a method for scheduling them called schedule background refresh. Find the applicationDidEnterBackground method and extension delegate. Close the console to give yourself some room. Add the following method just after the print. WKExtension.shared().scheduleBackground. You want the scheduleBackgroundRefresh. You'll see we have three parameters. The first is the preferred date. I'll make a date 15 seconds from now to use for this, and I'll put that right above it as refresh date. So let refreshDate = Date, and I'll use timeIntervalSinceNow, and that'll be 15.0 for 15 seconds. Then I can put into Date for the scheduleBackgroundRefresh refresh date. The emphasis here is on preferred. This is a suggestion. The system will do the background update when it thinks it's appropriate. The second parameter is userInfo. You can send the task any information you wish identifying this task it might need when you do the background refresh. If you do not have any user info, you can leave it nil. I'll make it nil for now. The last is a closure for error handling. So click on that and press return, and you'll get the closure. I'll make error named error for the argument, and then in here, I'll do if let error = error, which, if I have a non-nil value, I will have an unwrapped error. And then I'll just print that out as a localized description. And that will start a task. You handle a task once the system gets to it. Down at the bottom of the extension delegate is that one method we have yet to talk about, handle. Handle gets a set of background tasks and the template sets up the basic code for handling the different kinds of background tasks. It is a for loop that iterates through all the tasks in the set, and a switch statement which handles the different types of background tasks. We'll concern ourselves with the first one of these, the refresh background task. Before we do anything else, notice the comment in the line of code already here. You must release the background task with this. The template has it here so you don't forget. Above it, we can put some code. I'll copy the code from above for getting the view controller. An application did become active for getting the interface controller. Paste it down here, and then change the line inside to print the count to the console. I'll do a print interfaceController.count. I'll repeat the background task by scheduling another one by copying that first one we had up above. Command+C for copy, and then paste it down here under my interface controller line. Command+V for paste. Before you run, reset and start the stopwatch. Pop up the stopwatch, reset. I'm going to start it here. Now go back and run, start the timer, and hit the side button. The stopwatch is in the active, and we can see ... Or in the inactive, but visual one. We're in the background here in the dock. Eventually, you'll get a message like the one I got on the screen here, which has a no such file or directory error. And if you go all the way up above that, you'll find that you actually have a count. It'll keep doing this, and it'll take about a minute for each background task to happen. So you can wait for another minute, and if we wait a full minute, we will see it do another background task. You can usually tell because these are so identical. This NSUnderlyingError will change. If you scroll up once that changes, you'll see the count is 84. If you go up above that, our original count was 24, we're 60 seconds later. Launching the background refresh is truly up to the system, and it really, really wants you to have at least a minute between your refreshes. So don't do what I did here and set it to 15 seconds. Instead, set it to at least a minute or more. There's a bigger reason for this. You have a limited number of high-priority tasks, and once you use them up for the day, your task will be demoted to low-priority. Try to use as few as you possibly can.

Contents