Instance Method

save(_:withCompletion:)

Saves the provided object to the HealthKit store.

Declaration

func save(_ object: HKObject, withCompletion completion: @escaping (Bool, Error?) -> Void)

Parameters

object

The HealthKit object to be saved. This object can be any concrete subclass of the HKObject class (any of the HKCategorySample, HKQuantitySample, HKCorrelation, or HKWorkout classes).

completion

A block that this method calls as soon as the save operation is complete. This block is passed the following parameters:

success

A Boolean value. This parameter contains true if the object was successfully saved to the HealthKit store; otherwise, false.

error

An error object. If an error occurred, this object contains information about the error; otherwise, it is set to nil.

Discussion

This method operates asynchronously. As soon as the save operation is finished, it calls the completion block on a background queue.

If your app has not requested permission to share the object’s data type, the method fails with an errorAuthorizationNotDetermined error. If your app has been denied permission to save the object’s data type, it fails with an errorAuthorizationDenied error. Saving an object with the same unique identifier as an object already in the HealthKit store fails with an errorInvalidArgument error.

In iOS 9.0 and later, saving an object to the HealthKit store sets the object’s sourceRevision property to a HKSourceRevision instance representing the saving app. On earlier versions of iOS, saving an object sets the source property to a HKSource instance instead. In both cases, these values are available only after the object is retrieved from the HealthKit store. The original object is not changed.

All samples retrieved by iOS 9.0 and later are given a valid sourceRevision property. If the sample was saved using an earlier version of iOS, the source revision’s version is set to nil.

See Also

Working with HealthKit Objects

func delete(HKObject, withCompletion: (Bool, Error?) -> Void)

Deletes the specified object from the HealthKit store.

func delete([HKObject], withCompletion: (Bool, Error?) -> Void)

Deletes the specified objects from the HealthKit store.

func deleteObjects(of: HKObjectType, predicate: NSPredicate, withCompletion: (Bool, Int, Error?) -> Void)

Deletes objects saved by this application that match the provided type and predicate.

func earliestPermittedSampleDate()

Returns the earliest date permitted for samples.

func save([HKObject], withCompletion: (Bool, Error?) -> Void)

Saves an array of objects to the HealthKit store.