Skip to content

Commit d502999

Browse files
committed
'Cancel' for PromiseKit -- update MapKit cancellable code for Swift 4.2 and Xcode 10
1 parent deaf9d7 commit d502999

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

Sources/MKDirections+Promise.swift

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ extension MKDirections {
1717
#if swift(>=4.2)
1818
/// Begins calculating the requested route information asynchronously.
1919
public func calculate() -> Promise<Response> {
20-
return Promise { calculate(completionHandler: $0.resolve) }
20+
return Promise<Response>(cancellableTask: MKDirectionsTask(self)) { calculate(completionHandler: $0.resolve) }
2121
}
2222

2323
/// Begins calculating the requested travel-time information asynchronously.
2424
public func calculateETA() -> Promise<ETAResponse> {
25-
return Promise { calculateETA(completionHandler: $0.resolve) }
25+
return Promise<ETAResponse>(cancellableTask: MKDirectionsTask(self)) { calculateETA(completionHandler: $0.resolve) }
2626
}
2727
#else
2828
/// Begins calculating the requested route information asynchronously.
@@ -58,6 +58,17 @@ private class MKDirectionsTask: CancellableTask {
5858
//////////////////////////////////////////////////////////// Cancellable wrappers
5959

6060
extension MKDirections {
61+
#if swift(>=4.2)
62+
/// Begins calculating the requested route information asynchronously.
63+
public func cancellableCalculate() -> CancellablePromise<Response> {
64+
return cancellable(calculate())
65+
}
66+
67+
/// Begins calculating the requested travel-time information asynchronously.
68+
public func cancellableCalculateETA() -> CancellablePromise<ETAResponse> {
69+
return cancellable(calculateETA())
70+
}
71+
#else
6172
/// Begins calculating the requested route information asynchronously.
6273
public func cancellableCalculate() -> CancellablePromise<MKDirectionsResponse> {
6374
return cancellable(calculate())
@@ -67,4 +78,5 @@ extension MKDirections {
6778
public func cancellableCalculateETA() -> CancellablePromise<MKETAResponse> {
6879
return cancellable(calculateETA())
6980
}
81+
#endif
7082
}

Sources/MKMapSnapshotter+Promise.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ extension MKMapSnapshotter {
1717
#if swift(>=4.2)
1818
/// Starts generating the snapshot using the options set in this object.
1919
public func start() -> Promise<Snapshot> {
20-
return Promise { start(completionHandler: $0.resolve) }
20+
return Promise<Snapshot>(cancellableTask: MKMapSnapshotterTask(self)) { start(completionHandler: $0.resolve) }
2121
}
2222
#else
2323
/// Starts generating the snapshot using the options set in this object.
@@ -48,8 +48,15 @@ private class MKMapSnapshotterTask: CancellableTask {
4848
//////////////////////////////////////////////////////////// Cancellable wrapper
4949

5050
extension MKMapSnapshotter {
51+
#if swift(>=4.2)
52+
/// Starts generating the snapshot using the options set in this object.
53+
public func cancellableStart() -> CancellablePromise<Snapshot> {
54+
return cancellable(start())
55+
}
56+
#else
5157
/// Starts generating the snapshot using the options set in this object.
5258
public func cancellableStart() -> CancellablePromise<MKMapSnapshot> {
5359
return cancellable(start())
5460
}
61+
#endif
5562
}

0 commit comments

Comments
 (0)