Skip to content

Commit

Permalink
Add optional encoding completion callback queue argument for multipar…
Browse files Browse the repository at this point in the history
…t form upload (#2620)

* Add optional encoding completion callback queue argument for multipart form upload

* Use queue argument of upload function when calling alternate upload function
  • Loading branch information
jaltreuter authored and jshier committed Nov 24, 2018
1 parent a5cd9e2 commit ed37179
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions Source/SessionManager.swift
Expand Up @@ -611,6 +611,7 @@ open class SessionManager {
to url: URLConvertible,
method: HTTPMethod = .post,
headers: HTTPHeaders? = nil,
queue: DispatchQueue? = nil,
encodingCompletion: ((MultipartFormDataEncodingResult) -> Void)?)
{
do {
Expand All @@ -620,10 +621,11 @@ open class SessionManager {
multipartFormData: multipartFormData,
usingThreshold: encodingMemoryThreshold,
with: urlRequest,
queue: queue,
encodingCompletion: encodingCompletion
)
} catch {
DispatchQueue.main.async { encodingCompletion?(.failure(error)) }
(queue ?? DispatchQueue.main).async { encodingCompletion?(.failure(error)) }
}
}

Expand Down Expand Up @@ -654,6 +656,7 @@ open class SessionManager {
multipartFormData: @escaping (MultipartFormData) -> Void,
usingThreshold encodingMemoryThreshold: UInt64 = SessionManager.multipartFormDataEncodingMemoryThreshold,
with urlRequest: URLRequestConvertible,
queue: DispatchQueue? = nil,
encodingCompletion: ((MultipartFormDataEncodingResult) -> Void)?)
{
DispatchQueue.global(qos: .utility).async {
Expand All @@ -677,7 +680,7 @@ open class SessionManager {
streamFileURL: nil
)

DispatchQueue.main.async { encodingCompletion?(encodingResult) }
(queue ?? DispatchQueue.main).async { encodingCompletion?(encodingResult) }
} else {
let fileManager = FileManager.default
let tempDirectoryURL = URL(fileURLWithPath: NSTemporaryDirectory())
Expand Down Expand Up @@ -713,7 +716,7 @@ open class SessionManager {
}
}

DispatchQueue.main.async {
(queue ?? DispatchQueue.main).async {
let encodingResult = MultipartFormDataEncodingResult.success(
request: upload,
streamingFromDisk: true,
Expand All @@ -733,7 +736,7 @@ open class SessionManager {
}
}

DispatchQueue.main.async { encodingCompletion?(.failure(error)) }
(queue ?? DispatchQueue.main).async { encodingCompletion?(.failure(error)) }
}
}
}
Expand Down

0 comments on commit ed37179

Please sign in to comment.