Skip to content

Instantly share code, notes, and snippets.

@atsukoba
Created September 30, 2021 06:08
Show Gist options
  • Save atsukoba/4db00d701c493850fd4f0e534de802a4 to your computer and use it in GitHub Desktop.
Save atsukoba/4db00d701c493850fd4f0e534de802a4 to your computer and use it in GitHub Desktop.
Synchronized request sending with APIKit in Swift5
import Foundation
import APIKit
extension Session {
open class func sendSync<T: Request>(_ request: T) -> Result<T.Response, SessionTaskError> {
var result: Result<T.Response, SessionTaskError>!
let semaphor = DispatchSemaphore(value: 0)
self.send(request, callbackQueue: .sessionQueue) { _result in
result = _result
semaphor.signal()
}
semaphor.wait()
return result
}
}
@atsukoba
Copy link
Author

switch Session.sendSync(
  SomeRequest
) {
  case .success( _):
      break
  case .failure(let err):
      break
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment