diff --git a/Cartfile b/Cartfile index 2bfea98..c517d21 100644 --- a/Cartfile +++ b/Cartfile @@ -1 +1,2 @@ -github "mxcl/PromiseKit" ~> 6.0 +#github "mxcl/PromiseKit" ~> 6.0 +github "dougzilla32/PromiseKit" "CoreCancel" diff --git a/Cartfile.resolved b/Cartfile.resolved index a1699c6..80a4000 100644 --- a/Cartfile.resolved +++ b/Cartfile.resolved @@ -1 +1 @@ -github "mxcl/PromiseKit" "6.4.1" +github "dougzilla32/PromiseKit" "087b3cf470890ff9ea841212e2f3e285fecf3988" diff --git a/Sources/HealthKit+Promise.swift b/Sources/HealthKit+Promise.swift index 03029c4..9cad459 100644 --- a/Sources/HealthKit+Promise.swift +++ b/Sources/HealthKit+Promise.swift @@ -31,9 +31,15 @@ public extension HKStatisticsQuery { } public extension HKAnchoredObjectQuery { + /// - Note: cancelling this promise will cancel the underlying task + /// - SeeAlso: [Cancellation](http://promisekit.org/docs/) static func promise(type: HKSampleType, predicate: NSPredicate? = nil, anchor: HKQueryAnchor? = nil, limit: Int = HKObjectQueryNoLimit, healthStore: HKHealthStore = .init()) -> Promise<([HKSample], [HKDeletedObject], HKQueryAnchor)> { - return Promise { seal in - let query = HKAnchoredObjectQuery(type: type, predicate: predicate, anchor: anchor, limit: limit) { + var query: HKAnchoredObjectQuery! + var reject: ((Error) -> Void)! + + let promise = Promise<([HKSample], [HKDeletedObject], HKQueryAnchor)> { seal in + reject = seal.reject + query = HKAnchoredObjectQuery(type: type, predicate: predicate, anchor: anchor, limit: limit) { if let a = $1, let b = $2, let c = $3 { seal.fulfill((a, b, c)) } else if let e = $4 { @@ -44,13 +50,18 @@ public extension HKAnchoredObjectQuery { } healthStore.execute(query) } + + promise.setCancellableTask(LongRunningQuery(healthStore: healthStore, query: query), reject: reject) + return promise } } public extension HKStatisticsCollectionQuery { + /// - Note: cancelling this promise will cancel the underlying task + /// - SeeAlso: [Cancellation](http://promisekit.org/docs/) func promise(healthStore: HKHealthStore = .init()) -> Promise { - return Promise { seal in + return Promise(cancellableTask: LongRunningQuery(healthStore: healthStore, query: self)) { seal in initialResultsHandler = { seal.resolve($1, $2) } @@ -69,3 +80,20 @@ public extension HKSampleQuery { } } } + +class LongRunningQuery: CancellableTask { + let healthStore: HKHealthStore + let query: HKQuery + + init(healthStore: HKHealthStore, query: HKQuery) { + self.healthStore = healthStore + self.query = query + } + + func cancel() { + healthStore.stop(query) + isCancelled = true + } + + var isCancelled = false +}