title: Healthkit background delivery when device is locked with passcode
body: <p>I am using HealthKit framework in my application to get users daily step count from HealthApp.</p>
  <p>To save fetched step count from HealthApp, I am using Core Data.</p>
  <p>My requirement is, I want to keep HealthApp step count and Core data in sync. How to keep both in sync.</p>
  <p>Currently problem is, If iPhone is locked with passcode then background query will not be called? What is solution for this?</p>
  <p>Code:</p>
  <p>func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -&gt; Bool {</p>
  <pre><code>    updateOnBackground()
      return true
  </code></pre>
  <p>}</p>
  <pre><code>func updateOnBackground() {
         
         let stepType = HKQuantityType.quantityType(forIdentifier: .stepCount)!
      
         self.healthStore.enableBackgroundDelivery(for: stepType, frequency: .immediate) { (success, error) in
             if let error = error {
                 print(&quot;enableBackgroundDelivery error : \(error)&quot;)
             }
             if success {
                 print(&quot;background delivery enabled&quot;)
             }
         }
    
          let query = HKObserverQuery(sampleType: stepType, predicate: nil) { (query, completionHandler, error) in
               self.fetchHealthData() {
                   completionHandler()
               }
           }
           healthStore.execute(query)
  }
  </code></pre>
  <p>I have read below answers but unable to get solution for my requirement,</p>
  <p><a href="https://stackoverflow.com/questions/26375767/healthkit-background-delivery-when-app-is-not-running/35073904#35073904">Healthkit background delivery when app is not running</a></p>
  <p><a href="https://stackoverflow.com/questions/30361033/healthkit-data-inaccessible-in-background">HealthKit data inaccessible in background</a></p>