从HealthKit删除水样本 [英] Deleting a Water Sample from HealthKit

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

问题描述

我的应用程序当前允许用户保存WaterIntakeRecord,然后将其保留在Core Data中.我能够写HealthKit,将取水记录保存为HKQuantitySample.

My application currently lets users save WaterIntakeRecords and they are then persisted with Core Data. I am able to write to HealthKit, saving a water intake record as a HKQuantitySample.

我在应用程序中添加了WaterIntakeRecord,其量为12.9液量盎司.由于在健康"中,我以毫升为首选计量单位,因此以毫升为单位:

I add a WaterIntakeRecord into my application with an amount of 12.9 fluid ounces. Since in Health, I have milliliters as my preferred unit of measurement, it shows in milliliters:

我在尝试删除样本时遇到的困难.

Where I am struggling is when trying to delete a sample.

当用户保存WaterIntakeRecord时,他们可以选择要保存样品的计量单位,然后将其转换为美国盎司,这是WaterIntakeRecord的属性.这样,每个WaterIntakeRecord都有一个一致的度量单位,可以将其转换为其他度量单位(在Health中找到的所有度量单位)进行显示.

When the user saves a WaterIntakeRecord, they can choose the unit of measurement that they want to save the sample in, and then it converts that measurement to US ounces, which is a property of WaterIntakeRecord. This way, every WaterIntakeRecord has a consistent unit of measurement that can be converted into other units of measurement (all the ones found in Health) for display.

尝试删除保存的样本时,请尝试以下操作:

When trying to delete a saved sample, I try this:

static func deleteWaterIntakeSampleWithAmount(amount: Double, date: NSDate) {

    guard let type = HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDietaryWater) else {
        print("———> Could not retrieve quantity type for identifier")
        return
    }

    let quantity = HKQuantity(unit: HKUnit.fluidOunceUSUnit(), doubleValue: amount)

    let sample = HKQuantitySample(type: type, quantity: quantity, startDate: date, endDate: date)

    HealthKitStore.deleteObject(sample) { (success, error) -> Void in
        if let err = error {
            print("———> Could not delete water intake sample from HealthKit: \(err)")
        } else {
            print("———> Deleted water intake sample from HealthKit")
        }
    }
}

当用户删除我的应用程序中的记录时,应删除HealthKit中的相应示例.记录已成功从我的应用程序中删除,但是,使用上述方法尝试从HealthKit中删除样本时,我始终收到错误消息:

When the user deletes a record in my application, it should delete the corresponding sample in HealthKit. The record is successfully deleted from my application, however, I keep getting an error when attempting to delete the sample from HealthKit, using the method above:

Error Domain=com.apple.healthkit Code=100 "Transaction failure." UserInfo {NSLocalizedDescription=Transaction failure.}

我不确定自己在做什么,以保持收到此错误.

I'm not sure what I am doing incorrectly to keep getting this error.

amount参数是WaterIntakeRecordouncesUS值,而date参数是WaterIntakeRecord s的date属性,它是记录的创建日期:

The amount parameter is the WaterIntakeRecord's ouncesUS value, and the date parameter is the WaterIntakeRecords date property which is the creation date of the record:

关于导致删除失败的原因有什么想法?

Any ideas on where I am causing the deletion to fail?

推荐答案

HealthKit中的每个样本都是唯一的.删除样本时,必须首先查询应用程序最初保存的样本,然后在调用HealthKitStore.deleteObject()时使用它.在上面的代码段中,您正在创建一个未保存的新示例,然后尝试将其删除.

Each sample in HealthKit is unique. When deleting a sample, you must first query for the sample that your app saved originally and then use it in your call to HealthKitStore.deleteObject(). In your code snippet above, you are creating a new, unsaved sample then attempting to delete it.

这篇关于从HealthKit删除水样本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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