Skip to content

Anyone could help me how to delete the sample by HealthKitReporter? #19

Description

@drainlin

This is my code and I want to delete the sample that I created before.
But I got this error such as follows:

Optional(Error Domain=com.apple.healthkit Code=3 "Failed to find some objects for deletion." UserInfo={NSLocalizedDescription=Failed to find some objects for deletion.})

Thank you!

private func deleteSample(){
    do {
        let reporter = try HealthKitReporter()
        let types = [QuantityType.bloodGlucose]
        reporter.manager.requestAuthorization(
            toRead: types,
            toWrite: types
        ) { success, error in
            if success, error == nil {
                reporter.manager.preferredUnits(for: types) { preferredUnits, error in
                    if error == nil {
                        for preferredUnit in preferredUnits {
                            do {
                                let query = try reporter.reader.quantityQuery(
                                    type: try QuantityType.make(from: preferredUnit.identifier),
                                    unit: unit == 1 ? "mmol<180.1558800000541>/L" : "mg/dL"
                                ) { results, error in
                                    if error == nil {
                                        
                                        for item in results{
                                            reporter.writer.delete(sample: item ) { success, error in
                                                print(item)
                                                print(success)
                                                print(error)
                                            }
                                        }
                                            
                                        
                                    } else {
                                        print("error")
                                    }
                                }
                                reporter.manager.executeQuery(query)
                            } catch {
                                print(error)
                            }
                        }
                    } else {
                        print("error")
                    }
                }
            } else {
                print("error")
            }
        }
    } catch {
        print(error)
    }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions