由于未捕获的异常'

编程入门 行业动态 更新时间:2024-10-23 23:31:52
由于未捕获的异常'_HKObjectValidationFailureException'而终止应用(Terminating app due to uncaught exception '_HKObjectValidationFailureException')

我将血糖值保存到iOS中的Health Kit应用程序中。

-(void)viewDidLoad { float bloodGlucose=20; float bloodGlucoseValue= (int) bloodGlucose; NSLog(@"Blood Glucose value is :%f",bloodGlucoseValue); HKQuantityType *bloodGlucoseType=[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBloodGlucose]; NSSet *requesObjects=[[NSSet alloc]initWithObjects:bloodGlucoseType, nil]; HKHealthStore * hStore = [HKHealthStore new]; [hStore requestAuthorizationToShareTypes: requesObjects readTypes:requesObjects completion:^(BOOL success, NSError *error) { //user response processing goes here, i.e. if(success) { dispatch_async(dispatch_get_main_queue(), ^{ [[GSHealthKitManager sharedManager]saveBloodGlucosePercentageintoHealthStore:bloodGlucoseValue]; }); } }]; } -(void)saveBloodGlucosePercentageintoHealthStore:(float)bloodGlucoseValue { HKUnit *inches=[HKUnit gramUnit]; HKQuantity * glucoseQuantity = [HKQuantity quantityWithUnit:inches doubleValue:bloodGlucoseValue]; HKQuantityType *glucoseType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBloodGlucose]; NSDate *now = [NSDate date]; HKQuantitySample *bloodGlucosesample = [HKQuantitySample quantitySampleWithType:glucoseType quantity:glucoseQuantity startDate:now endDate:now]; [self.healthStore saveObject:bloodGlucosesample withCompletion:^(BOOL success, NSError *error){ if (!success) { NSLog(@"Error"); } }]; }

如果我运行应用程序,我在这行代码中遇到以下异常:

HKQuantitySample *bloodGlucosesample = [HKQuantitySample quantitySampleWithType:glucoseType quantity:glucoseQuantity startDate:now endDate:now];

跟踪:

Exception is: Terminating app due to uncaught exception '_HKObjectValidationFailureException', reason: 'HKQuantitySample 20 g 2016-04-07 15:39:44 +0530 2016-04-07 15:39:44 +0530 requires unit of type Mass/Volume. Incompatible unit: g' *** First throw call stack: ( 0 CoreFoundation 0x00b73a14 __exceptionPreprocess + 180 1 libobjc.A.dylib 0x00634e02 objc_exception_throw + 50 2 CoreFoundation 0x00b7393d +[NSException raise:format:] + 141 3 HealthKit 0x0010da70 -[HKObject _validateForCreation] + 149 4 HealthKit 0x0010d89e +[HKObject _newDataObjectWithMetadata:device:config:] + 273 5 HealthKit 0x0014e661 +[HKSample _newSampleWithType:startDate:endDate:device:metadata:config:] + 200 6 HealthKit 0x0013244f +[HKQuantitySample quantitySampleWithType:quantity:startDate:endDate:device:metadata:] + 324 7 HealthKit 0x0013265a +[HKQuantitySample quantitySampleWithType:quantity:startDate:endDate:] + 116 8 HealthKitProto 0x0005cda2 -[GSHealthKitManager saveBloodGlucosePercentageintoHealthStore:] + 274 9 HealthKitProto 0x0005f4c7 __34-[FirstViewController viewDidLoad]_block_invoke_9 + 103 10 libdispatch.dylib 0x0367a377 _dispatch_call_block_and_release + 15 11 libdispatch.dylib 0x0369d9cd _dispatch_client_callout + 14 12 libdispatch.dylib 0x03682f90 _dispatch_main_queue_callback_4CF + 910 13 CoreFoundation 0x00ac4fde __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 14 14 CoreFoundation 0x00a82cd4 __CFRunLoopRun + 2356 15 CoreFoundation 0x00a820e6 CFRunLoopRunSpecific + 470 16 CoreFoundation 0x00a81efb CFRunLoopRunInMode + 123 17 GraphicsServices 0x04523664 GSEventRunModal + 192 18 GraphicsServices 0x045234a1 GSEventRun + 104 19 UIKit 0x00f02bfa UIApplicationMain + 160 20 HealthKitProto 0x000603ba main + 138 21 libdyld.dylib 0x036c7a21 start + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException

I am saving the blood glucose values into Health Kit application in iOS.

-(void)viewDidLoad { float bloodGlucose=20; float bloodGlucoseValue= (int) bloodGlucose; NSLog(@"Blood Glucose value is :%f",bloodGlucoseValue); HKQuantityType *bloodGlucoseType=[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBloodGlucose]; NSSet *requesObjects=[[NSSet alloc]initWithObjects:bloodGlucoseType, nil]; HKHealthStore * hStore = [HKHealthStore new]; [hStore requestAuthorizationToShareTypes: requesObjects readTypes:requesObjects completion:^(BOOL success, NSError *error) { //user response processing goes here, i.e. if(success) { dispatch_async(dispatch_get_main_queue(), ^{ [[GSHealthKitManager sharedManager]saveBloodGlucosePercentageintoHealthStore:bloodGlucoseValue]; }); } }]; } -(void)saveBloodGlucosePercentageintoHealthStore:(float)bloodGlucoseValue { HKUnit *inches=[HKUnit gramUnit]; HKQuantity * glucoseQuantity = [HKQuantity quantityWithUnit:inches doubleValue:bloodGlucoseValue]; HKQuantityType *glucoseType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBloodGlucose]; NSDate *now = [NSDate date]; HKQuantitySample *bloodGlucosesample = [HKQuantitySample quantitySampleWithType:glucoseType quantity:glucoseQuantity startDate:now endDate:now]; [self.healthStore saveObject:bloodGlucosesample withCompletion:^(BOOL success, NSError *error){ if (!success) { NSLog(@"Error"); } }]; }

If I run the application, I got following exception in this line of code:

HKQuantitySample *bloodGlucosesample = [HKQuantitySample quantitySampleWithType:glucoseType quantity:glucoseQuantity startDate:now endDate:now];

Trace:

Exception is: Terminating app due to uncaught exception '_HKObjectValidationFailureException', reason: 'HKQuantitySample 20 g 2016-04-07 15:39:44 +0530 2016-04-07 15:39:44 +0530 requires unit of type Mass/Volume. Incompatible unit: g' *** First throw call stack: ( 0 CoreFoundation 0x00b73a14 __exceptionPreprocess + 180 1 libobjc.A.dylib 0x00634e02 objc_exception_throw + 50 2 CoreFoundation 0x00b7393d +[NSException raise:format:] + 141 3 HealthKit 0x0010da70 -[HKObject _validateForCreation] + 149 4 HealthKit 0x0010d89e +[HKObject _newDataObjectWithMetadata:device:config:] + 273 5 HealthKit 0x0014e661 +[HKSample _newSampleWithType:startDate:endDate:device:metadata:config:] + 200 6 HealthKit 0x0013244f +[HKQuantitySample quantitySampleWithType:quantity:startDate:endDate:device:metadata:] + 324 7 HealthKit 0x0013265a +[HKQuantitySample quantitySampleWithType:quantity:startDate:endDate:] + 116 8 HealthKitProto 0x0005cda2 -[GSHealthKitManager saveBloodGlucosePercentageintoHealthStore:] + 274 9 HealthKitProto 0x0005f4c7 __34-[FirstViewController viewDidLoad]_block_invoke_9 + 103 10 libdispatch.dylib 0x0367a377 _dispatch_call_block_and_release + 15 11 libdispatch.dylib 0x0369d9cd _dispatch_client_callout + 14 12 libdispatch.dylib 0x03682f90 _dispatch_main_queue_callback_4CF + 910 13 CoreFoundation 0x00ac4fde __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 14 14 CoreFoundation 0x00a82cd4 __CFRunLoopRun + 2356 15 CoreFoundation 0x00a820e6 CFRunLoopRunSpecific + 470 16 CoreFoundation 0x00a81efb CFRunLoopRunInMode + 123 17 GraphicsServices 0x04523664 GSEventRunModal + 192 18 GraphicsServices 0x045234a1 GSEventRun + 104 19 UIKit 0x00f02bfa UIApplicationMain + 160 20 HealthKitProto 0x000603ba main + 138 21 libdyld.dylib 0x036c7a21 start + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException

最满意答案

似乎克不是healthkit支持的单位。

检查SDK文档中的允许单位和此处

编辑:您为此值指定一个坏单位

HKUnit *inches=[HKUnit gramUnit]; // this is not the right unit (nor inches) HKQuantity * glucoseQuantity = [HKQuantity quantityWithUnit:inches doubleValue:bloodGlucoseValue];

您应该创建一个质量单位和体积单位,然后将第一个除以第二个(在swift中):

let milligramPerDeciLiter = HKUnit.gramUnitWithMetricPrefix(.Milli).unitDividedByUnit(HKUnit.literUnitWithMetricPrefix(.Deci))

在Objc:

HKUnit* milligramPerDeciLiter = [[HKUnit gramUnitWithMetricPrefix:HKMetricPrefixMilli] unitDividedByUnit:[HKUnit literUnitWithMetricPrefix:HKMetricPrefixDeci]];

It seems that grams is not an unit supported by healthkit.

Check the allowed units in the SDK documentation and here

Edit: You specify a bad unit for this value

HKUnit *inches=[HKUnit gramUnit]; // this is not the right unit (nor inches) HKQuantity * glucoseQuantity = [HKQuantity quantityWithUnit:inches doubleValue:bloodGlucoseValue];

You should create a mass unit and volume unit then divide the first by the second one (in swift):

let milligramPerDeciLiter = HKUnit.gramUnitWithMetricPrefix(.Milli).unitDividedByUnit(HKUnit.literUnitWithMetricPrefix(.Deci))

In Objc :

HKUnit* milligramPerDeciLiter = [[HKUnit gramUnitWithMetricPrefix:HKMetricPrefixMilli] unitDividedByUnit:[HKUnit literUnitWithMetricPrefix:HKMetricPrefixDeci]];

更多推荐

本文发布于:2023-07-31 20:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1347354.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:异常

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!