I am using HealthKit framework in my application to get users daily step count from HealthApp.
To save fetched step count from HealthApp, I am using Core Data.
My requirement is, I want to keep HealthApp step count and Core data in sync. How to keep both in sync.
Currently problem is, If iPhone is locked with passcode then background query will not be called? What is solution for this?
Code:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
updateOnBackground()
return true
}
func updateOnBackground() {
let stepType = HKQuantityType.quantityType(forIdentifier: .stepCount)!
self.healthStore.enableBackgroundDelivery(for: stepType, frequency: .immediate) { (success, error) in
if let error = error {
print("enableBackgroundDelivery error : \(error)")
}
if success {
print("background delivery enabled")
}
}
let query = HKObserverQuery(sampleType: stepType, predicate: nil) { (query, completionHandler, error) in
self.fetchHealthData() {
completionHandler()
}
}
healthStore.execute(query)
}
I have read below answers but unable to get solution for my requirement,