草庐IT

ios - 由于未捕获的异常 '_HKObjectValidationFailureException' 而终止应用程序

coder 2024-01-11 原文

我正在将血糖值保存到 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

最佳答案

好像克不是healthkit支持的单位。

检查 SDK documentation 中允许的单位和 here

编辑:您为此值指定了错误的单位

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

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

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

在对象中:

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

关于ios - 由于未捕获的异常 '_HKObjectValidationFailureException' 而终止应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36473745/

有关ios - 由于未捕获的异常 '_HKObjectValidationFailureException' 而终止应用程序的更多相关文章

  1. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  2. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  3. ruby-on-rails - 'compass watch' 是如何工作的/它是如何与 rails 一起使用的 - 2

    我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t

  4. ruby - 将差异补丁应用于字符串/文件 - 2

    对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl

  5. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  6. ruby-on-rails - Rails 应用程序之间的通信 - 2

    我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此

  7. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  8. ruby - 在 jRuby 中使用 'fork' 生成进程的替代方案? - 2

    在MRIRuby中我可以这样做:deftransferinternal_server=self.init_serverpid=forkdointernal_server.runend#Maketheserverprocessrunindependently.Process.detach(pid)internal_client=self.init_client#Dootherstuffwithconnectingtointernal_server...internal_client.post('somedata')ensure#KillserverProcess.kill('KILL',

  9. ruby-on-rails - Rails 应用程序中的 Rails : How are you using application_controller. rb 是新手吗? - 2

    刚入门rails,开始慢慢理解。有人可以解释或给我一些关于在application_controller中编码的好处或时间和原因的想法吗?有哪些用例。您如何为Rails应用程序使用应用程序Controller?我不想在那里放太多代码,因为据我了解,每个请求都会调用此Controller。这是真的? 最佳答案 ApplicationController实际上是您应用程序中的每个其他Controller都将从中继承的类(尽管这不是强制性的)。我同意不要用太多代码弄乱它并保持干净整洁的态度,尽管在某些情况下ApplicationContr

  10. ruby - 主要 :Object when running build from sublime 的未定义方法 `require_relative' - 2

    我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby​​1.9+ 关于ruby-主要:Objectwhenrun

随机推荐