在 watchOS 4 Beta 4 中将数据插入 HKWorkoutRouteBuilder 时出现令人费解的错误

Puzzling error inserting data into HKWorkoutRouteBuilder in watchOS 4 Beta 4

当我将路线数据插入 HKWorkoutRouteBuilder 时,出现以下错误:

与名为 com.apple.healthd.server 的服务的连接已中断,但消息是通过其他代理发送的,因此该代理已失效

这是一个代码片段。

    workoutRouteBuilder.insertRouteData(filteredLocations) { (success, error) in
        if !success {
            print("inserting route data failed with error: \(String(describing: error))")
        }
    }

我根据 Speed Sloth 示例中的实现进行了建模。

如有任何见解,我们将不胜感激!

更新: 这是来自监视日志的更多信息。看起来像是某种权限问题,但我还没能找到它:

fault 13:21:14.664262 -0400 healthd connection from pid 1705: Warning: Exception caught during invocation of received message, dropping incoming message and invalidating the connection. Exception: Invalid parameter not satisfying: [authorizationStatuses count] == [typesIncludingParentTypes count] Invalid parameter not satisfying: [authorizationStatuses count] == [typesIncludingParentTypes count] ( 0 CoreFoundation
0x1dc04d25 + 153 1 libobjc.A.dylib
0x1d227181 objc_exception_throw + 39 2 CoreFoundation
0x1dc04be5 + 1 3 Foundation
0x1e43c1cd + 93 4 HealthDaemon
0x2edf678f + 2015 5 HealthDaemon
0x2ee6eed1 + 143 6 HealthDaemon
0x2ee6ec77 + 143 7 HealthDaemon
0x2ee7b99b + 485 8 HealthDaemon
0x2f0e1e1f + 143 9 Foundation
0x1e589393 + 19 10 Foundation
0x1e587<…>

The connection to service named com.apple.healthd.server was interrupted, but the message was sent over an additional proxy and therefore this proxy has become invalid

看到此消息表示处理请求的系统进程已崩溃或退出。您应该向 Apple 提交错误。查找 healthd 崩溃日志并将其包括在内。

我遇到了同样的问题。您需要确保向用户请求授权:

// Objective C
[HKSeriesType workoutRouteType]

// Swift
HKSeriesType.workoutRoute()

不要忘记在 AppDelegate 为 HKHealthStore 添加 WorkoutRoute 请求

锻炼路线不适用于模拟器,仅适用于某些 HKWorkoutActivityType,例如 .walking, .运行 在 OS4

private func requestAccessToHealthKit() {

    let healthStore = HKHealthStore()

    let allTypes = Set([HKObjectType.workoutType(),
                        HKObjectType.quantityType(forIdentifier: .activeEnergyBurned)!,
                        HKObjectType.quantityType(forIdentifier: .distanceWalkingRunning)!])
    if #available(watchOS 4.0, *) {
        allTypes.insert(HKSeriesType.workoutRoute())
    }
    healthStore.requestAuthorization(toShare: allTypes, read: allTypes) { (success, error) in
        if !success {
            print(error?.localizedDescription ?? "")
        }
    }
}