【问题标题】:App does not receive DeviceMotion updates in background (WatchOS 6)应用程序不会在后台接收 DeviceMotion 更新 (WatchOS 6)
【发布时间】:2020-07-20 23:40:07
【问题描述】:

我正在为 Apple Watch 开发一个应用程序,该应用程序通过 DeviceMotion 获取加速度计、陀螺仪数据,并通过 HKWorkout 获取心率。

在应用程序停留在前台之前,一切正常。但是,当它进入非活动状态时(当用户降低手腕时),它会工作一段时间,但在某个点它会停止。此外,当应用程序进入后台时,应用程序停止收集新的 DeviceMotion 更新,但我认为心率观察器继续工作,因为绿色 LED 仍然亮着。

我用于 DeviceMotion 的代码是:

if self.motion.isDeviceMotionAvailable {
     self.motion.deviceMotionUpdateInterval = 1.0 / 100.0
     self.motion.showsDeviceMovementDisplay = true

     self.motion.startDeviceMotionUpdates(using: .xMagneticNorthZVertical, to: OperationQueue.main, withHandler: { (data, error) in
          if let d = data {
               //collecting data
          }
     }
}

我用于心率的代码:

let workoutConfiguration = HKWorkoutConfiguration()
workoutConfiguration.activityType = .crossTraining
workoutConfiguration.locationType = .indoor

do {
     if workoutSession == nil {
          workoutSession = try HKWorkoutSession(healthStore: HealthDataManager.sharedInstance.healthStore!, configuration: workoutConfiguration)
               workoutSession?.startActivity(with: Date())
          }
} catch {
      print("Error starting workout session: \(error.localizedDescription)")
}

HealthDataManager.sharedInstance.observeHeartRateSamples { (heartRate) -> (Void) in
      print("heart rate sample: \(heartRate)")
      self.lastHearRateSample = heartRate
}

由于 Apple 文档声明 (https://developer.apple.com/documentation/healthkit/workouts_and_activity_rings)

当锻炼会话处于活动状态时,您的应用可以继续在后台运行。这使您的应用程序可以监控用户并在整个活动中收集数据。此外,它还能确保您的应用在用户查看手表时出现。

我认为在这些情况下无需管理应用程序的行为。但是,似乎必须做点什么。在后台状态(和非活动状态)收集新的 DeviceMotion 数据的最佳方式是什么?

【问题讨论】:

    标签: swift background watchkit apple-watch devicemotion


    【解决方案1】:

    事实证明,由于健康包锻炼,该应用程序进入了后台。但是,我使用的资源过多,系统暂停了该应用程序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-18
      • 1970-01-01
      • 2013-10-13
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      相关资源
      最近更新 更多