1

I'm developing an independent watch app on XCode 11.0 beta 5. Everything works alright except background refresh. I'm using the following code to schedule the background refresh task when I open the app:

let fireDate = Date(timeIntervalSinceNow: 60.0 * 30.0)
// optional, any SecureCoding compliant data can be passed here
let userInfo = ["reason" : "update UI"] 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.")
      }
}

The func handle(_ backgroundTasks: Set<WKRefreshBackgroundTask>) is never called. If I use the Debug->Simulate background fetch option from XCode the method gets called.

Jelly
  • 4,522
  • 6
  • 26
  • 42

2 Answers2

1

I have exactly the same problem, even if I don't run the app as a standalone. The problem only occurred with watchOS 6.

Does anyone have an idea what the solution is?

Here is my source code:

import WatchKit

class ExtensionDelegate: NSObject, WKExtensionDelegate {

func applicationDidFinishLaunching() {
    // Perform any final initialization of your application.
    print("applicationDidFinishLaunching")
    self.reloadActiveComplications()
    scheduleNextReload()
}

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.
    self.reloadActiveComplications()
    scheduleNextReload()
}

func applicationWillResignActive() {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, etc.
}

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.
    print("background")
    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.
            scheduleNextReload()
            self.reloadActiveComplications()
            backgroundTask.setTaskCompletedWithSnapshot(true)
        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.setTaskCompletedWithSnapshot(true)
        case let urlSessionTask as WKURLSessionRefreshBackgroundTask:
            // Be sure to complete the URL session task once you’re done.
            urlSessionTask.setTaskCompletedWithSnapshot(true)
        default:
            // make sure to complete unhandled task types
            task.setTaskCompletedWithSnapshot(true)
        }
    }
}

func reloadActiveComplications() {
    let server = CLKComplicationServer.sharedInstance()

    print("ExtensionDelegate: requesting reload of complications")
    for complication in server.activeComplications ?? [] {
        server.reloadTimeline(for: complication)
    }
}

func scheduleNextReload() {
    var targetDate:Date
    let currentDate = Date()

    let timezoneOffset =  TimeZone.current.secondsFromGMT()
    let epochDate = currentDate.timeIntervalSince1970
    let timezoneEpochOffset = (epochDate + Double(timezoneOffset))
    let timeZoneOffsetDate = Date(timeIntervalSince1970: timezoneEpochOffset)

    targetDate = timeZoneOffsetDate.addingTimeInterval(120)
    print("ExtensionDelegate: scheduling next update at %@", "\(timeZoneOffsetDate)")
    print("ExtensionDelegate: scheduling next update at %@", "\(targetDate)")

    WKExtension.shared().scheduleBackgroundRefresh(
        withPreferredDate: targetDate,
        userInfo: nil,
        scheduledCompletion: { error in
            // contrary to what the docs say, this is called when the task is scheduled, i.e. immediately
            NSLog("ExtensionDelegate: background task %@",
                  error == nil ? "scheduled successfully" : "NOT scheduled: \(error!)")
        }
    )
}

}

Andre Bongartz
  • 101
  • 1
  • 1
  • 8
  • It was a device debug only problem for me, try it on the simulator. On the simulator it worked for me. – Jelly Oct 21 '19 at 12:51
  • I'm seeing the same problem. It seems that from watchOS6 WKRefreshBackgroundTask is not working fine. – tokentoken Nov 14 '19 at 15:20
  • Any luck in getting WKRefreshBackgroundTask to work in WatchOS6 by any of you? I'm hitting this problem now too. – Ryan Dec 29 '19 at 05:09
  • 2
    As of Xcode 11.3 and WatchOS 6.1.1, WKExtension.shared().scheduleBackgroundRefresh(withPreferredDate: Date(timeIntervalSinceNow: interval), userInfo: userInfo) runs without error in the Watchkit Extension Delegate (in did finish launching). But handle(_ backgroundTasks: Set) is never called on Simulator! – BlueskyMed Dec 30 '19 at 21:25
  • 1
    Any news on this? – Kevin Lieser Mar 26 '20 at 21:11
  • Update. Now using Xcode 11.5, the schedule background task and hand background seem to work flawlessly on Xcode Sim. However, on series 5 device hardware, execution is flaky and seldom if ever runs a scheduled background task for my app. The scenario is that i have a task updating a complication gauge. First run 20 s after launch, thereafter 31 min interval. Never runs after first time. – BlueskyMed May 24 '20 at 12:46
1

Try scheduling it in applicationDidResignActive, rather than from a controller.

It only does it IF the application is in the background. It doesn't seem to think it needs to do it if the application isn't in the background.

You can make applicationDidResignActive fire by pressing the crown button.

BenTaylor
  • 414
  • 4
  • 12