0

I want to query steps from HealthKit every 15mins to update my Complication via WidgetKit.

In the Timeline Provider getTimeline() I execute the query, create one timeline entry with the steps and set timeline refresh policy to .after(now + 15min).

Currently I am stuck because the refresh is never triggered. The steps are queried and shown after initially setting the complication on the Watchface but never refresh.

`

    func getTimeline(in context: Context, completion: @escaping (Timeline<HealthKitWidgetEntry>) -> Void) {
        let currentDate = Date()
        let refreshMinuteGranuity = 15
        let refreshDate = Calendar.current.date(
            byAdding: .minute,
            value: refreshMinuteGranuity,
            to: currentDate
        )!
        
        healthData.getTodaysSteps { steps in
            let entry = HealthKitWidgetEntry(
                date: currentDate, steps: steps
            )
            let timeline = Timeline(
                entries: [entry], policy: .after(refreshDate)
            )
            print("Next refresh: \(refreshDate)")
            completion(timeline)
        }
    }

`

Any input on how to solve this?

1 Answers1

0

I had the same problem. I was able to work around it by starting an observer like this: enableBackgroundDelivery Health-kit iOS15 not working correctly

In the ChangeHandler you can cause the widget to update itself:

WidgetCenter.shared.reloadAllTimelines()

Be sure you set the Entitlement for "HealthKit Observer Query Background Delivery"

SvenS
  • 1