HKObserverQuery后台模式

时间:2018-12-26 11:47:21

标签: ios swift background fetch

我的ios应用程序中的healthkit后台传递存在一些问题。我尝试每小时获取一次步数,但只有在应用程序首次运行时才能够这样做(它向我显示了我的打印件,该交付已启用)。在那之后,我的查询再也没有调用过。可以,请您告诉我该怎么做。也许我做错了,这是我的代码:

let sampleType =  HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)


        let query: HKObserverQuery = HKObserverQuery(sampleType: sampleType!, predicate: nil, updateHandler: { (observerQuery, handler, error) in
            doSomeOperations()
            handler()
        })

        healthKitStore.execute(query)
        healthKitStore.enableBackgroundDelivery(for: sampleType!, frequency: .hourly, withCompletion: {(succeeded: Bool, error: Error!) in

            if succeeded{

                print("Enabled background delivery of step changes")
            } else {
                if let theError = error{
                    print("Failed to enable background delivery of step changes. ")
                    print("Error = \(theError)")
                }
            }
            } )

0 个答案:

没有答案