You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Eliminate duplicate code in the cancellable extensions
* When using the 'cancellable' function with the extensions, ensure that the underlying task (if any) is always cancelled
* When using the 'cancellable' function, call 'reject' on the underlying promise (wherever possible) if 'cancel' is invoked
* Create cancellable wrappers for the extensions only for the cases where there is an underlying task that supports cancellation
/// - Remark: we force a `to` parameter because Apple deletes the downloaded file immediately after the underyling completion handler returns.
95
125
/// - Note: we do not create the destination directory for you, because we move the file with FileManager.moveItem which changes it behavior depending on the directory status of the URL you provide. So create your own directory first!
96
126
publicfunc downloadTask(_:PMKNamespacer, with convertible:URLRequestConvertible, to saveLocation:URL)->Promise<(saveLocation:URL, response:URLResponse)>{
97
-
returnPromise{ seal in
98
-
downloadTask(with: convertible.pmkRequest, completionHandler:{ tmp, rsp, err in
127
+
vartask:URLSessionTask!
128
+
varreject:((Error)->Void)!
129
+
130
+
letpromise= Promise<(saveLocation: URL, response: URLResponse)>{ seal in
131
+
reject = seal.reject
132
+
task =self.downloadTask(with: convertible.pmkRequest, completionHandler:{ tmp, rsp, err in
99
133
iflet error = err {
100
134
seal.reject(error)
101
135
}elseiflet rsp = rsp,let tmp = tmp {
@@ -108,8 +142,12 @@ extension URLSession {
108
142
}else{
109
143
seal.reject(PMKError.invalidCallingConvention)
110
144
}
111
-
}).resume()
145
+
})
146
+
task.resume()
112
147
}
148
+
149
+
promise.setCancellableTask(task, reject: reject)
150
+
return promise
113
151
}
114
152
}
115
153
@@ -238,15 +276,15 @@ public extension Promise where T == (data: Data, response: URLResponse) {
/// Wraps the (Data?, URLResponse?, Error?) response from URLSession.uploadTask(with:from:) as CancellablePromise<(Data,URLResponse)>
348
376
publicfunc cancellableUploadTask(_:PMKNamespacer, with convertible:URLRequestConvertible, from data:Data)->CancellablePromise<(data:Data, response:URLResponse)>{
Wraps the URLSesstionDownloadTask response from URLSession.downloadTask(with:) as CancellablePromise<(URL,URLResponse)>
379
387
- Remark: we force a `to` parameter because Apple deletes the downloaded file immediately after the underyling completion handler returns.
380
388
*/
381
389
publicfunc cancellableDownloadTask(_:PMKNamespacer, with convertible:URLRequestConvertible, to saveLocation:URL)->CancellablePromise<(saveLocation:URL, response:URLResponse)>{
382
-
vartask:URLSessionTask!
383
-
varreject:((Error)->Void)!
384
-
385
-
letpromise= CancellablePromise<(saveLocation: URL, response: URLResponse)>{ seal in
386
-
reject = seal.reject
387
-
task =self.downloadTask(with: convertible.pmkRequest, completionHandler:{ tmp, rsp, err in
388
-
iflet error = err {
389
-
seal.reject(error)
390
-
}elseiflet rsp = rsp,let tmp = tmp {
391
-
do{
392
-
tryFileManager.default.moveItem(at: tmp, to: saveLocation)
0 commit comments