代码之家  ›  专栏  ›  技术社区  ›  GarySabo

尝试删除hkworkout时,healthkit错误“未能找到要删除的某些对象”

  •  1
  • GarySabo  · 技术社区  · 6 年前

    在尝试“修改”一个 HKWorkout 通过(1)复制现有训练(2)更新元数据(3)删除“旧”训练(在下面的“我的代码”中选择的训练),然后(4)保存新修改的训练。98%的时间,这段代码工作完美,但2%的时间,我会得到这个错误,我结束了重复的训练。我做错什么了吗?

    作为旁白,我真的希望healthkit能让我们修改数据,这样就不必跳这个舞了。

    class func updateMetadataDeleteOldAndSaveNewWorkout(selectedWorkout: HKWorkout, handler: @escaping (Bool,WorkoutManagerError? ) -> Void) {   
    
        //configure metadata   
    
        //  Create a new workout with the old workout's fields and new edited metadata object
                    let newWorkout = HKWorkout(activityType: selectedWorkout.workoutActivityType, start: selectedWorkout.startDate, end: selectedWorkout.endDate, duration: selectedWorkout.duration, totalEnergyBurned: selectedWorkout.totalEnergyBurned, totalDistance: selectedWorkout.totalDistance, metadata: metadata)
    
                    // Delete the old workout
                    HealthStoreSingleton.sharedInstance.healthStore.delete(selectedWorkout, withCompletion: { (success, error) in
    
                        DispatchQueue.main.async {
    
                         if let unwrappedError = error  {
                             handler(false, WorkoutManagerError.deleteError(unwrappedError.localizedDescription))
                            return 
                         }
                        }
    
                        //  When delete was successful save the new workout
                        HealthStoreSingleton.sharedInstance.healthStore.save(newWorkout) { success, error in
    
                            DispatchQueue.main.async {
                                if let unwrappedError = error {
                                    handler(false, WorkoutManagerError.saveError(unwrappedError.localizedDescription))
                                    return 
                                }
    
                                if success {
    
                                    handler(true, nil)
                                    return 
                                } else {
                                      handler(false, WorkoutManagerError.saveError("\(String(describing: error))"))
                                      return 
                                }
                            }
                        }
                    })
    
    0 回复  |  直到 6 年前
    推荐文章