2016-06-21 66 views
0

我想記錄一個3位的NSNumber健康套件作爲PeakExpiratoryFlowRate,代碼如下保存PeakExpiratoryFlowRate HealthKit記錄

- (void)PeakFlowupdate:(NSNumber *)pkFlow 
{ 

    NSString *identifier = HKQuantityTypeIdentifierPeakExpiratoryFlowRate; 
    HKQuantityType *peakFlowType = [HKObjectType quantityTypeForIdentifier:identifier]; 

    HKQuantity *myPeakFlow = [HKQuantity quantityWithUnit:[HKUnit minuteUnit] doubleValue:[pkFlow doubleValue]]; 
    HKQuantitySample *peakFlowSample = [HKQuantitySample quantitySampleWithType:peakFlowType quantity:myPeakFlow startDate:[NSDate date] endDate:[NSDate date]]; 

    [healthStore saveObject:peakFlowSample withCompletion:^(BOOL success, NSError *error) { 

     if (success) { 
      NSLog(@"SAVED!"); 
     } else { 
      NSLog(@"%@", error); 
     } 

    }]; 
} 

但是當我嘗試將數據保存到醫療包我收到此錯誤

終止應用程序由於未捕獲的異常 「_HKObjectValidationFailureException」,原因:「HKQuantitySample 250 分鐘2016年6月22日10時32分24秒1200 2016年6月22日10時32分24秒1200需要單元類型爲Volume/Time的 。不兼容的單位:分鐘

回答

0

由於異常狀態的消息,您不能實例化峯值流量樣本,其數量以分鐘爲單位指定。峯值流量的單位是體積/時間,因此您需要使用諸如[[HKUnit literUnit] unitDividedByUnit:[HKUnit minuteUnit]]之類的東西。