是否有必要在连续删除然后保存HKWorkout时调度到HealthKit中的主队列?

时间:2018-02-25 04:51:31

标签: ios swift grand-central-dispatch watch-os

我的代码在手表上,正在使用现有的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])
                                }
                            }
                        }
                    }
                }

0 个答案:

没有答案