ios - 从 HealthKit 读取锻炼数据的最佳方法

标签 ios swift healthkit

我想将锻炼的心率信息上传到我的服务器。问题是,我不确定如何获取此信息。

当我结束锻炼时,我会保存心率和能量消耗信息。我可以在这里包含其他代码,这些代码也将此信息写入核心数据,然后基本上立即将其拉出并与 iOS 应用程序同步,后者与服务器同步。这是最好的方法吗?

在 iOS 中是否可以查询 HealthKit 锻炼类(class),并在每次更新时提取心率读数列表?

不是很相关,但对于我在上面提到的即时同步的一些澄清。它不会总是立即与服务器同步,因为当 watch 超出范围时,用户可以创建其他锻炼类(class),这些类(class)的相关数据存储在核心数据中,然后在回到范围内时立即同步。

最佳答案

When I end the workout, I save the heart rate, and energy burned info. I could include other code here that also writes this information to Core Data, then basically immediately pulls it out and syncs it with the iOS app, which syncs it with the server. Is this the best approach?

我建议创建一些 HealthKitService,它负责获取数据并将其保存到 HKHealthStorage。 使用 PONSO 对象来描述您将在应用的不同层中传递的 DTO

它看起来像这样:

struct Workout {
    let date: Date
    let sets: Double
    let reps: Double
    let heartRate: Double
}

protocol HealthKitService: class {
    func saveWorkout(_ workout: Workout, completion: (() -> ())?)
    func fetchWorkouts(by date: Date, completion: (([Workout]) -> ())?)
}

为简单起见,如果您的数据模型与 HKWorkout 没有区别,我建议不要使用 Core Data,但如果您需要保留一些与锻炼相关的额外数据,而 不支持这些数据>HealthKit 然后你必须设置一个自己的存储。

如果您想将用户的健康数据发送到您自己的服务器,请检查 Protecting User Privacy文档以防止您的应用在 App Store 审查期间被拒绝。

In iOS is it possible to query HealthKit workout sessions, and extract a list of heart rate readings at each update?

是的,这是可能的。查看 HKWorkout 的官方文档. 基本上,您需要一个 HKHealthStorage 来将与 HKWorkout 相关的心率保存为 HKSample。 然后你可以执行查询来接收数据,这里是一个reference .

关于ios - 从 HealthKit 读取锻炼数据的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50584543/

相关文章:

android - websocket 是否专用于浏览器

ios - 如何在 Swift 中初始化 AWSCognitoIdentityUserAttributeType?

ios - 我可以在没有 HealthKit 的情况下使用 ResearchKit 吗?

ios - 在 HealthKit 中查询 heartRateVariabilitySDNN?

ios - 在 Swift 中单击 TableView 的单元格时如何调用(显示)另一个 View Controller

iphone - 嵌入 YouTube 视频

ios - 无法在 iOS 模拟器上以 Facebook 测试帐户登录

ios - 单按不能与双按组合使用

swift Playground : Show Line Execution Counts > i. e。 (# 次)

ios - 使用 Swift 从 HealthKit 读取日期间隔的步骤