Saving 'HKQuantitySample' to 'HKHealthStore' does not update its 'sourceRevision' inside 'completion'

huangapple go评论56阅读模式
英文:

Saving 'HKQuantitySample' to 'HKHealthStore' does not update its 'sourceRevision' inside 'completion'

问题

当我通过在HKHealthStore.save(_:withCompletion:)withCompletion中创建HKQuantitySample来将新数据保存到HealthKit时,当我检查sourceRevision时,它是<uninitialized>

以下是我的代码:

let dietaryWaterQuantity = HKQuantity(unit: HKUnit.literUnit(with: .milli), doubleValue: 100)
let dietaryWaterSample = HKQuantitySample(type: HKSampleType.quantityType(forIdentifier: .dietaryWater)!,
                                          quantity: dietaryWaterQuantity,
                                          start: Date(),
                                          end: Date())

HKHealthStore().save(dietaryWaterSample) { success, error in
   /// dietaryWaterSample.sourceRevision is <uninitialized> so I cannot use dietaryWaterSample.sourceRevision.source.bundleIdentifier
}

欢迎任何想法。

英文:

When I save new data to HealthKit by creating HKQuantitySample inside the withCompletion of HKHealthStore.save(_:withCompletion:) when I check sourceRevision it is <uninitialized>:

Here is my code:

let dietaryWaterQuantity = HKQuantity(unit: HKUnit.literUnit(with: .milli), doubleValue: 100)
let dietaryWaterSample = HKQuantitySample(type: HKSampleType.quantityType(forIdentifier: .dietaryWater)!,
                                          quantity: dietaryWaterQuantity,
                                          start: Date(),
                                          end: Date())

HKHealthStore().save(dietaryWaterSample) { success, error in
   /// dietaryWaterSample.sourceRevision is <uninitialized> so I cannot use dietaryWaterSample.sourceRevision.source.bundleIdentifier
}

Any ideas are welcomed.

答案1

得分: 1

根据sourceRevision属性的文档(重点标记是我的):

>sourceRevision属性仅在您从HealthKit存储中检索到的对象上可用。当您创建新对象时,sourceRevision被设置为nil。系统会自动将此属性设置为表示将对象保存到HealthKit存储的应用的当前版本。然后,sourceRevision在下次从存储中检索对象时可用。

根据最后一句话,您必须在访问有效的sourceRevision之前从存储中检索对象。原始保存的实例不会通过调用save进行更新。

英文:

From the documentation for the sourceRevision property (emphasis mine):

>The source revision property is only available on objects you have retrieved from the HealthKit store. When you create a new object, the source revision is set to nil. The system automatically sets this property to represent the current version of the app that saved the object to the HealthKit store. The source revision is then available the next time the object is retrieved from the store.

Based on that last statement, you must retrieve the object from the store before you will have access to a valid sourceRevision. The original saved instance is not updated via a call to save.

huangapple
  • 本文由 发表于 2023年2月27日 02:38:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75574220.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定