英文:
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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论