Skip to content

Improve threading #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 12, 2021
Merged
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
2 changes: 1 addition & 1 deletion .codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ coverage:
status:
patch:
default:
target: auto
target: 67
changes: false
project:
default:
Expand Down
100 changes: 32 additions & 68 deletions Sources/ParseSwift/API/API+Commands.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ internal extension API {

// MARK: Synchronous Execution
func executeStream(options: API.Options,
callbackQueue: DispatchQueue,
childObjects: [String: PointerType]? = nil,
childFiles: [UUID: ParseFile]? = nil,
uploadProgress: ((URLSessionTask, Int64, Int64, Int64) -> Void)? = nil,
Expand All @@ -61,6 +62,7 @@ internal extension API {
let task = URLSession.parse.uploadTask(withStreamedRequest: urlRequest)
ParseConfiguration.sessionDelegate.uploadDelegates[task] = uploadProgress
ParseConfiguration.sessionDelegate.streamDelegates[task] = stream
ParseConfiguration.sessionDelegate.taskCallbackQueues[task] = callbackQueue
task.resume()
return
}
Expand All @@ -70,6 +72,7 @@ internal extension API {
}

func execute(options: API.Options,
callbackQueue: DispatchQueue,
childObjects: [String: PointerType]? = nil,
childFiles: [UUID: ParseFile]? = nil,
uploadProgress: ((URLSessionTask, Int64, Int64, Int64) -> Void)? = nil,
Expand All @@ -78,7 +81,7 @@ internal extension API {
let group = DispatchGroup()
group.enter()
self.executeAsync(options: options,
callbackQueue: nil,
callbackQueue: callbackQueue,
childObjects: childObjects,
childFiles: childFiles,
uploadProgress: uploadProgress,
Expand All @@ -97,7 +100,8 @@ internal extension API {

// MARK: Asynchronous Execution
// swiftlint:disable:next function_body_length cyclomatic_complexity
func executeAsync(options: API.Options, callbackQueue: DispatchQueue?,
func executeAsync(options: API.Options,
callbackQueue: DispatchQueue,
childObjects: [String: PointerType]? = nil,
childFiles: [UUID: ParseFile]? = nil,
uploadProgress: ((URLSessionTask, Int64, Int64, Int64) -> Void)? = nil,
Expand All @@ -114,18 +118,9 @@ internal extension API {
switch result {

case .success(let decoded):
if let callbackQueue = callbackQueue {
callbackQueue.async { completion(.success(decoded)) }
} else {
completion(.success(decoded))
}

completion(.success(decoded))
case .failure(let error):
if let callbackQueue = callbackQueue {
callbackQueue.async { completion(.failure(error)) }
} else {
completion(.failure(error))
}
completion(.failure(error))
}
}
case .failure(let error):
Expand All @@ -140,26 +135,20 @@ internal extension API {

case .success(let urlRequest):

URLSession.parse.uploadTask(with: urlRequest,
from: uploadData,
from: uploadFile,
progress: uploadProgress,
mapper: mapper) { result in
URLSession
.parse
.uploadTask(callbackQueue: callbackQueue,
with: urlRequest,
from: uploadData,
from: uploadFile,
progress: uploadProgress,
mapper: mapper) { result in
switch result {

case .success(let decoded):
if let callbackQueue = callbackQueue {
callbackQueue.async { completion(.success(decoded)) }
} else {
completion(.success(decoded))
}

completion(.success(decoded))
case .failure(let error):
if let callbackQueue = callbackQueue {
callbackQueue.async { completion(.failure(error)) }
} else {
completion(.failure(error))
}
completion(.failure(error))
}
}
case .failure(let error):
Expand All @@ -173,24 +162,18 @@ internal extension API {
childFiles: childFiles) {

case .success(let urlRequest):
URLSession.parse.downloadTask(with: urlRequest,
progress: downloadProgress,
mapper: mapper) { result in
URLSession
.parse
.downloadTask(callbackQueue: callbackQueue,
with: urlRequest,
progress: downloadProgress,
mapper: mapper) { result in
switch result {

case .success(let decoded):
if let callbackQueue = callbackQueue {
callbackQueue.async { completion(.success(decoded)) }
} else {
completion(.success(decoded))
}

completion(.success(decoded))
case .failure(let error):
if let callbackQueue = callbackQueue {
callbackQueue.async { completion(.failure(error)) }
} else {
completion(.failure(error))
}
completion(.failure(error))
}
}
case .failure(let error):
Expand All @@ -202,18 +185,9 @@ internal extension API {
switch result {

case .success(let decoded):
if let callbackQueue = callbackQueue {
callbackQueue.async { completion(.success(decoded)) }
} else {
completion(.success(decoded))
}

completion(.success(decoded))
case .failure(let error):
if let callbackQueue = callbackQueue {
callbackQueue.async { completion(.failure(error)) }
} else {
completion(.failure(error))
}
completion(.failure(error))
}
}
} else {
Expand Down Expand Up @@ -596,8 +570,7 @@ internal extension API {
var responseResult: Result<U, ParseError>?
let group = DispatchGroup()
group.enter()
self.executeAsync(options: options,
callbackQueue: nil) { result in
self.executeAsync(options: options) { result in
responseResult = result
group.leave()
}
Expand All @@ -611,7 +584,7 @@ internal extension API {
}

// MARK: Asynchronous Execution
func executeAsync(options: API.Options, callbackQueue: DispatchQueue?,
func executeAsync(options: API.Options,
completion: @escaping(Result<U, ParseError>) -> Void) {

switch self.prepareURLRequest(options: options) {
Expand All @@ -620,18 +593,9 @@ internal extension API {
switch result {

case .success(let decoded):
if let callbackQueue = callbackQueue {
callbackQueue.async { completion(.success(decoded)) }
} else {
completion(.success(decoded))
}

completion(.success(decoded))
case .failure(let error):
if let callbackQueue = callbackQueue {
callbackQueue.async { completion(.failure(error)) }
} else {
completion(.failure(error))
}
completion(.failure(error))
}
}
case .failure(let error):
Expand Down
23 changes: 18 additions & 5 deletions Sources/ParseSwift/API/ParseURLSessionDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class ParseURLSessionDelegate: NSObject, URLSessionDelegate, URLSessionDataDeleg
var downloadDelegates = [URLSessionDownloadTask: ((URLSessionDownloadTask, Int64, Int64, Int64) -> Void)]()
var uploadDelegates = [URLSessionTask: ((URLSessionTask, Int64, Int64, Int64) -> Void)]()
var streamDelegates = [URLSessionTask: InputStream]()
var taskCallbackQueues = [URLSessionTask: DispatchQueue]()

init (callbackQueue: DispatchQueue,
authentication: ((URLAuthenticationChallenge,
Expand All @@ -35,7 +36,9 @@ class ParseURLSessionDelegate: NSObject, URLSessionDelegate, URLSessionDataDeleg
completionHandler: @escaping (URLSession.AuthChallengeDisposition,
URLCredential?) -> Void) {
if let authentication = authentication {
authentication(challenge, completionHandler)
callbackQueue.async {
authentication(challenge, completionHandler)
}
} else {
completionHandler(.performDefaultHandling, nil)
}
Expand All @@ -46,8 +49,10 @@ class ParseURLSessionDelegate: NSObject, URLSessionDelegate, URLSessionDataDeleg
didSendBodyData bytesSent: Int64,
totalBytesSent: Int64,
totalBytesExpectedToSend: Int64) {
if let callBack = uploadDelegates[task] {
callbackQueue.async {
if let callBack = uploadDelegates[task],
let queue = taskCallbackQueues[task] {

queue.async {
callBack(task, bytesSent, totalBytesSent, totalBytesExpectedToSend)

if totalBytesSent == totalBytesExpectedToSend {
Expand All @@ -63,8 +68,9 @@ class ParseURLSessionDelegate: NSObject, URLSessionDelegate, URLSessionDataDeleg
totalBytesWritten: Int64,
totalBytesExpectedToWrite: Int64) {

if let callBack = downloadDelegates[downloadTask] {
callbackQueue.async {
if let callBack = downloadDelegates[downloadTask],
let queue = taskCallbackQueues[downloadTask] {
queue.async {
callBack(downloadTask, bytesWritten, totalBytesWritten, totalBytesExpectedToWrite)
if totalBytesWritten == totalBytesExpectedToWrite {
self.downloadDelegates.removeValue(forKey: downloadTask)
Expand All @@ -77,6 +83,7 @@ class ParseURLSessionDelegate: NSObject, URLSessionDelegate, URLSessionDataDeleg
downloadTask: URLSessionDownloadTask,
didFinishDownloadingTo location: URL) {
downloadDelegates.removeValue(forKey: downloadTask)
taskCallbackQueues.removeValue(forKey: downloadTask)
}

func urlSession(_ session: URLSession,
Expand All @@ -86,4 +93,10 @@ class ParseURLSessionDelegate: NSObject, URLSessionDelegate, URLSessionDataDeleg
completionHandler(stream)
}
}

func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
uploadDelegates.removeValue(forKey: task)
streamDelegates.removeValue(forKey: task)
taskCallbackQueues.removeValue(forKey: task)
}
}
4 changes: 4 additions & 0 deletions Sources/ParseSwift/API/URLSession+extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ extension URLSession {

extension URLSession {
internal func uploadTask<U>( // swiftlint:disable:this function_parameter_count
callbackQueue: DispatchQueue,
with request: URLRequest,
from data: Data?,
from file: URL?,
Expand All @@ -126,11 +127,13 @@ extension URLSession {
}
if let task = task {
ParseConfiguration.sessionDelegate.uploadDelegates[task] = progress
ParseConfiguration.sessionDelegate.taskCallbackQueues[task] = callbackQueue
task.resume()
}
}

internal func downloadTask<U>(
callbackQueue: DispatchQueue,
with request: URLRequest,
progress: ((URLSessionDownloadTask, Int64, Int64, Int64) -> Void)?,
mapper: @escaping (Data) throws -> U,
Expand All @@ -142,6 +145,7 @@ extension URLSession {
responseError: responseError, mapper: mapper))
}
ParseConfiguration.sessionDelegate.downloadDelegates[task] = progress
ParseConfiguration.sessionDelegate.taskCallbackQueues[task] = callbackQueue
task.resume()
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/ParseSwift/LiveQuery/LiveQuerySocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ final class LiveQuerySocket: NSObject {
func closeAll() {
delegates.forEach { (_, client) -> Void in
client.close(useDedicatedQueue: false)
authenticationDelegate = nil
}
authenticationDelegate = nil
}
}

Expand Down
Loading