gpt4 book ai didi

swift - 使用 HealthKit 后台交付检索步骤后,在后台将数据写入 Firebase

转载 作者:搜寻专家 更新时间:2023-11-01 06:16:18 25 4
gpt4 key购买 nike

我有一个 HKObserverQuery 设置来在后台获取步骤(enableBackgroundDelivery 方法在 application:didFinishLaunchingWithOptions: 中调用)。

步骤是在后台检索的,但我还想将检索到的步骤存储在 Firebase 数据库中。虽然这部分失败了,但 Firebase 中没有存储任何内容。当应用程序位于前台时,存储步骤的方法确实可以正常工作。任何关于如何在后台成功地在 Firebase 中写入数据的想法将不胜感激。

class HealthKitManager {

static let shared = HealthKitManager()
private let healthStore = HKHealthStore()
private let stepsQuantityType = HKQuantityType.quantityType(forIdentifier: .stepCount)!

private init() {

}

func getTodaysStepCount(completion: @escaping (Double) -> Void) {
let now = Date()
let startOfDay = Calendar.current.startOfDay(for: now)
let predicate = HKQuery.predicateForSamples(withStart: startOfDay, end: now, options: .strictStartDate)

let query = HKStatisticsQuery(quantityType: stepsQuantityType, quantitySamplePredicate: predicate, options: .cumulativeSum) { (_, result, error) in
var resultCount = 0.0

guard let result = result else {
log.error("Failed to fetch steps = \(error?.localizedDescription ?? "N/A")")
completion(resultCount)
return
}

if let sum = result.sumQuantity() {
resultCount = sum.doubleValue(for: HKUnit.count())
}

DispatchQueue.main.async {
completion(resultCount)
}
}

healthStore.execute(query)
}

func enableBackgroundDelivery() {
let query = HKObserverQuery(sampleType: stepsQuantityType, predicate: nil) { [weak self] (query, completionHandler, error) in
if let error = error {
log.error("Observer query failed = \(error.localizedDescription)")
return
}

self?.getTodaysStepCount(completion: { steps in
// Store steps using Firebase:
StepsManager.shared.updateUserSteps(steps)
completionHandler()
})
}

healthStore.execute(query)
healthStore.enableBackgroundDelivery(for: stepsQuantityType, frequency: .hourly) { (success, error) in
log.debug("Background delivery of steps. Success = \(success)")

if let error = error {
log.error("Background delivery of steps failed = \(error.localizedDescription)")
}
}
}

}

最佳答案

好的,我解决了这个问题。问题是我正在调用 completionHandler,告诉 HealthKit 我已经完成了我的操作,然后才真正完成保存到 Firebase。保存到 Firebase 是异步完成的。

我向 StepsManager.shared.updateUserSteps 函数添加了一个完成处理程序:

func updateUserSteps(_ steps: Double, completion: (() -> Void)? = nil) {
let stepsReference = databaseInstance.reference(withPath: stepsPath)
stepsReference.setValue(steps) { _, _ in
completion?()
}
}

databaseRef.setValue 完成时触发。然后我将观察者查询更新为以下内容:

self?.getTodaysStepCount(completion: { steps in
StepsManager.shared.updateUserSteps(steps) {
completionHandler() // the HKObserverQueryCompletionHandler
}
})

Firebase 操作现在正确完成。

关于swift - 使用 HealthKit 后台交付检索步骤后,在后台将数据写入 Firebase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44241093/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com