I'm using HealthKit
data in my widget. If the phone is locked, it's not possible to get HealthKit
data, only if the phone is unlocked. However, my widget timeline's will try to update even if the phone is locked.
Is it possible to return an empty completion somehow, so it will keep the current widget data untouched?
This is my code:
struct Provider: IntentTimelineProvider {
private let healthKitService = HealthKitService()
func getTimeline(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
let currentDate = Date()
let refreshDate = Calendar.current.date(byAdding: .minute, value: 5, to: currentDate)!
healthKitService.getHeartRate() { data, error in
//Create an empty entry
var entry = SimpleEntry(date: currentDate, configuration: ConfigurationIntent(), data: nil)
//If no errors, set data
if(error == nil) {
entry.data = data
} else {
print(error) //this runs when a locked phone does the widget update
}
//Return response
let timeline = Timeline(entries: [entry], policy: .after(refreshDate))
completion(timeline)
}
}
}
What I can do is to store the entry data in UserDefaults
and load that up in the error route? I'm not sure if thats a good solution though.