Unable to fetch Water data from HealthKit

Hi All,

I am posting this to get some help on fetching Water intake data from Health Kit.

I have done (following a similar approach, and perfectly working) the fetch of the user weight, but for the water, somehow, I always receive back 0 samples.

I went to the Health App, added few entries (in different days) for the water intake, under the food sections.

Then, I use the following snippet to read the values, but I always have 0 values back.

What I am doing wrong?

            let type = HKQuantityType(.dietaryWater)
                        
            let samplePredicate = HKSamplePredicate.sample(type: type, predicate: nil)
            
            // Create the descriptor.
            let descriptor = HKSampleQueryDescriptor(
                predicates: [samplePredicate],
                sortDescriptors: [SortDescriptor(\.endDate, order: .reverse)])
            
            let results = try await descriptor.result(for: hkHealthStore)
            

The results variable is always 0 elements.

What I am doing wrong?

PS: The permissions are checked and correctly given for this: HKQuantityType(.dietaryWater)

Answered by SkinSoftware in 799726022

Found the issue:

I added the HKQuantityType(.dietaryWater) request in the

try await hkHealthStore.requestAuthorization(toShare: writeValues, read: readValues)

As share variable (thinking this would include the read) but it doesn't read.

If others have the same issue. Double check that 😉

Accepted Answer

Found the issue:

I added the HKQuantityType(.dietaryWater) request in the

try await hkHealthStore.requestAuthorization(toShare: writeValues, read: readValues)

As share variable (thinking this would include the read) but it doesn't read.

If others have the same issue. Double check that 😉

Unable to fetch Water data from HealthKit
 
 
Q