Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A fix for the broken implementation of HKHealhStore.deleteObjectsOfType in iOS 9.0 (see radar 22977320)
//
// HKHealthStore-DeleteObjects.swift
// HealthKitTest
//
// Created by Ben Dodson on 06/10/2015.
// Copyright © 2015 Dodo Apps. All rights reserved.
//
import Foundation
import HealthKit
extension HKHealthStore {
func deleteSamplesOfType(sampleType: HKSampleType, predicate: NSPredicate, withCompletion completion: (success: Bool, count: Int, error: NSError?) -> Void) {
let query = HKSampleQuery(sampleType: sampleType, predicate: predicate, limit: 0, sortDescriptors: nil) { (query, results, error) -> Void in
if let _ = error {
completion(success: false, count: 0, error: error)
return
}
if let objects = results {
if objects.count == 0 {
completion(success: true, count: 0, error: nil)
} else {
self.deleteObjects(objects, withCompletion: { (success, error) -> Void in
completion(success: error == nil, count: objects.count, error: error)
})
}
} else {
completion(success: true, count: 0, error: nil)
}
}
self.executeQuery(query)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.