Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cartfile
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
github "mxcl/PromiseKit" ~> 6.0
#github "mxcl/PromiseKit" ~> 6.0
github "dougzilla32/PromiseKit" "CoreCancel"
2 changes: 1 addition & 1 deletion Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1 +1 @@
github "mxcl/PromiseKit" "6.4.1"
github "dougzilla32/PromiseKit" "087b3cf470890ff9ea841212e2f3e285fecf3988"
34 changes: 31 additions & 3 deletions Sources/HealthKit+Promise.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<HKStatisticsCollection> {
return Promise { seal in
return Promise(cancellableTask: LongRunningQuery(healthStore: healthStore, query: self)) { seal in
initialResultsHandler = {
seal.resolve($1, $2)
}
Expand All @@ -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
}