HealthKit:与提供的锻炼相关的样本删除问题 [英] HealthKit : Issue with associated samples deletion of provided workout

查看:57
本文介绍了HealthKit:与提供的锻炼相关的样本删除问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用HealthKit,我正在保存以下数据:

Using HealthKit, I am saving below data:

  1. 锻炼
  2. 有功能源
  3. 距离

我正在使用以下代码删除锻炼内容:

I am deleting workout using below code:

self.healthStore?.delete(workout, withCompletion: { (status, error) in

但是上面的代码只是从HealthKit应用程序中删除了锻炼.我想删除锻炼及其相关的样本.我该怎么办?

But above code just deletes a workout from HealthKit app. I want to delete workout and it's associated samples. How can i do this?

推荐答案

要删除关联的样本,我们需要对特定的HKQuantityTypeIdentifier执行删除查询.

To delete associated samples we need to perform delete query on specific HKQuantityTypeIdentifier.

要从锻炼中删除有功能量,请参考以下代码:

To delete Active Energy from Workout refer below code:

 let energyBurnedQuantity = HKQuantityType.quantityType(forIdentifier: .activeEnergyBurned)

    let predicate = HKQuery.predicateForObjects(from: workout)

    let energyQuery = HKSampleQuery(sampleType: energyBurnedQuantity!, predicate: predicate, limit: 100, sortDescriptors: nil) { (query, result, error) in

        if error == nil {
            guard let resultData = result else {
                return
            }

            if resultData.count > 0 {
                self.healthStore?.delete(resultData, withCompletion: { [unowned self] (status, error) in

                    if status == true {

                        print("Successfully deleted Energy.")
                        })
                    } else {
                        print("Error \(String(describing: error?.localizedDescription))")
                    }
                })
            }
        }
    }

    self.healthStore?.execute(energyQuery)

这篇关于HealthKit:与提供的锻炼相关的样本删除问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆