Last active
October 6, 2015 15:13
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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
For more back story on this gist: