Skip to content

Instantly share code, notes, and snippets.

@bendodson
Last active October 6, 2015 15:13
Show Gist options
  • Save bendodson/c0f0a6a1f601dc4573ba to your computer and use it in GitHub Desktop.
Save bendodson/c0f0a6a1f601dc4573ba to your computer and use it in GitHub Desktop.
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