我的代码在手表上,正在使用现有的HKWorkout,修改其中的值,然后重新保存。要做到这一点,我需要复制并删除旧的。在嵌入save
电话的delete
电话之前,是否需要发送到主队列?或者,如果足够我在任何UI调用之前调度到主队列?我注意到手表上的UI有一些奇怪的行为,我认为它可能与线程相关?请参阅下面标记的行:
healthStoreManager.deleteWorkout(workout: workoutSavedWithoutStatsUnwrapped) { (successDeleting, error) in
if let unwrappedError = error {
DispatchQueue.main.async {
let action2 = WKAlertAction(title: "OK", style: .default, handler: {
WKInterfaceController.reloadRootPageControllers(withNames: ["InterfaceController"],
contexts: nil,
orientation: .vertical,
pageIndex: 0)
print("Error Saving workout")
})
WKInterfaceDevice.current().play(.failure)
self.presentAlert(withTitle: "Add Stats Error", message: "We were not able to add stats to your workout at this time, please enter them on the iPhone. Error: \(unwrappedError)", preferredStyle: .alert, actions: [action2])
}
}
if successDeleting == true {
******DO I NEED TO `DispatchQueue.main.async {` here? ********
self.healthStoreManager.saveWorkoutWithMetaData(session: self.workoutSession, workoutStartDate: self.startDate) { (successSaving, error) in
if let unwrappedError = error {
DispatchQueue.main.async {
let action2 = WKAlertAction(title: "OK", style: .default, handler: {
print("Error Saving workout")
})
WKInterfaceDevice.current().play(.failure)
self.presentAlert(withTitle: "Save Error", message: "We were not able to add stats to your workout at this time, please enter them on the iPhone. Error: \(unwrappedError)", preferredStyle: .alert, actions: [action2])
}
}
if successSaving == true {
DispatchQueue.main.async {
WKInterfaceDevice.current().play(.success)
self.showSuccessAlertWith(message: "Workout Saved, Stats Added.")
}
} else {
DispatchQueue.main.async {
let action2 = WKAlertAction(title: "OK", style: .default, handler: {
print("Error Saving workout")
WKInterfaceController.reloadRootPageControllers(withNames: ["InterfaceController"],
contexts: nil,
orientation: .vertical,
pageIndex: 0)
})
WKInterfaceDevice.current().play(.failure)
self.presentAlert(withTitle: "Save Error", message: "We were not able to add stats to your workout at this time, please enter them on the iPhone.", preferredStyle: .alert, actions: [action2])
}
}
}
}
}