Contents

save(_:withCompletion:)

Saves the provided object to the HealthKit store.

Declaration

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

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.

Mentioned in

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 HKError.Code.errorAuthorizationNotDetermined error. If your app has been denied permission to save the object’s data type, it fails with an HKError.Code.errorAuthorizationDenied error. Saving an object with the same unique identifier as an object already in the HealthKit store fails with an HKError.Code.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