Swift can';t使用HKWORKIUTSESSION开始训练课程

Swift can';t使用HKWORKIUTSESSION开始训练课程,swift,apple-watch,healthkit,watchos,Swift,Apple Watch,Healthkit,Watchos,我正在尝试使用session.startActivity在Apple Watch中开始训练课程,但状态仍然为NotStarted(1)。 尽管如此,似乎锻炼确实开始了,因为我的应用程序在后台运行,而且我的手表上也显示了锻炼符号。 稍后,当我尝试使用session.end停止训练时,出现以下错误: "Unable to transition to the desired state from the NotStarted(1) state (event 6). Allowed trans

我正在尝试使用session.startActivity在Apple Watch中开始训练课程,但状态仍然为NotStarted(1)。 尽管如此,似乎锻炼确实开始了,因为我的应用程序在后台运行,而且我的手表上也显示了锻炼符号。 稍后,当我尝试使用session.end停止训练时,出现以下错误:

"Unable to transition to the desired state from the NotStarted(1) state (event 6). Allowed transitions from the current state are: {
7 = "<error(7): NotStarted(1) -> Ended(3)>";
1 = "<prepare(1): NotStarted(1) -> Prepared(5)>";
2 = "<start(2): NotStarted(1) -> Running(2)>";" 
“训练”-你确定它与开始训练的对象相同吗

开始后,当您稍后调用“训练”(状态:“停止”)时,您可以执行以下操作:

func workout(state: String) {
    
    do {
        session = try HKWorkoutSession(healthStore: healthStore, configuration: configuration)
这将覆盖您现有的会话对象,因此您尝试停止的是另一个会话,该会话从未启动,因此无法停止。

就是这样!谢谢
func workout(state: String) {
    
    do {
        session = try HKWorkoutSession(healthStore: healthStore, configuration: configuration)