result of closure in function to variable in swift 3 - swift3

I've got a function which does an asynchrone call
func getToken(completionHandler:#escaping (_ stringResponse: String) -> Void) {
var testResult: String! = "20,5º"
let url: String! = "https://the.base.url/token"
let parameters: Parameters = [
"key":"value"
]
Alamofire.request(url, method: .post, parameters: parameters, encoding: URLEncoding(destination: .methodDependent)).validate().responseJSON { response in
switch response.result {
case .success:
testResult = "21º"
case .failure(let error):
testResult = error as! String
}
completionHandler(testResult)
}
}
And I call this function
getToken(completionHandler: {
(stringResponse: String) in
print(stringResponse)
})
And it nicely prints 21º as I can see in the debugger. However, the final value of stringResponse should end up in
lblTemp.setText(String(//here the results of stringResponse))
How do I do this? I'm guessing it must be incredible simple.

You need to do it like that
getToken(completionHandler: { [weak self] (stringResponse: String) in
DispatchQueue.main.async {
self?.lblTemp.text = stringResponse
}
})

Related

SwiftUI - Publish Background Thread Not Allowed - on code that does not update ui

New to swiftui and don't understand why the JSONDecoder() line in the first code throws
[SwiftUI] Publishing changes from background threads is not allowed; make sure to publish values from the main thread (via operators like receive(on:)) on model updates.
This to me is not updating ui so why is this showing?
do {
// pass the request type, bearer token, and email / password ( which are sent as POST body params )
let request = L.getRequest(requestType:"POST", token: token, email: self.username, password: self.psw)
L.fetchData(from: request) { result in
switch result {
case .success(let data):
// covert the binary data to a swift dictionary
do {
let response = try JSONDecoder().decode(WpJson.self, from: data)
for (key, title) in response.allowedReadings {
let vimeoId = Int( key )!
let vimeoUri = self.buildVimeoUri(vimeoId: key)
self.addReadingEntity(vimeoUri: vimeoUri, vimeoId: vimeoId, title: title)
}
self.writeToKeychain(jwt:response.jwt, displayName: response.displayName)
readings = self.fetchReadings()
}
catch {
self.error = error.localizedDescription
}
case .failure(let error):
self.error = error.localizedDescription
}
}
}
I tried wrapping a main queue around the do-catch in the L.fetchData(from: request) { result in but this did not help
DispatchQueue.main.async { [weak self] in
Here is the Login protocol, again without any ui work:
import Foundation
import SwiftUI
struct Login: Endpoint {
var url: URL?
init(url: URL?) {
self.url = url
}
}
protocol Endpoint {
var url: URL? { get set }
init(url: URL?)
}
extension Endpoint {
func getRequestUrl() -> URLRequest {
guard let requestUrl = url else { fatalError() }
// Prepare URL Request Object
return URLRequest(url: requestUrl)
}
func getRequest(requestType:String="POST", token:String, email:String="", password:String="") -> URLRequest {
var request = self.getRequestUrl()
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
if ( "" != email && "" != password && requestType == "POST") {
let parameters:[String:String?] = [
"email": email,
"password": password
]
// Run the request
do {
// pass dictionary to nsdata object and set it as request body
request.httpBody = try JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted)
} catch let error {
print(error.localizedDescription)
}
}
return request;
}
func fetchData(from request: URLRequest, completion: #escaping (Result<Data, NetworkError>) -> Void) {
URLSession.shared.dataTask(with: request) { data, response, error in
if let data = data {
completion(.success(data))
} else if error != nil {
// any sort of network failure
completion(.failure(.requestFailed))
} else {
// this ought not to be possible, yet here we are
completion(.failure(.unknown))
}
}.resume()
}
}
extension URLSession {
func dataTask(with request: URLRequest, completionHandler: #escaping (Result<(Data, HTTPURLResponse), Error>) -> Void) -> URLSessionDataTask {
return dataTask(with: request, completionHandler: { (data, urlResponse, error) in
if let error = error {
completionHandler(.failure(error))
} else if let data = data, let urlResponse = urlResponse as? HTTPURLResponse {
completionHandler(.success((data, urlResponse)))
}
})
}
}
Do you have any idea on how to fix this?
Wrap it right in place of assignment
catch {
DispatchQueue.main.async {
self.error = error.localizedDescription
}
}
case .failure(let error):
DispatchQueue.main.async {
self.error = error.localizedDescription
}
}

Migrating Alamofire.Request extension from Swift 2 to Swift 3

I'm trying to migrate an extension to Alamofire.Request but am getting the error Cannot call value of non-function type 'HTTPURLResponse?'.
I know the compiler thinks I'm referring to the member response and not the function.
I've already replaced Request.JSONResponseSerializer with DataRequest.jsonResponseSerializer.
Can anyone see what I'm missing?
extension Alamofire.Request {
public func responseSwiftyJSON(_ queue: DispatchQueue? = nil, options: JSONSerialization.ReadingOptions = .allowFragments, completionHandler: #escaping (NSURLRequest, HTTPURLResponse?, SwiftyJSON.JSON, Error?) -> Void) -> Self {
return response(responseSerializer: Alamofire.DataRequest.jsonResponseSerializer(options: options)) { response in
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
var responseJSON: JSON
if response.result.isFailure {
responseJSON = JSON.null
} else {
responseJSON = SwiftyJSON.JSON(response.result.value!)
}
dispatch_async(queue ?? dispatch_get_main_queue(), {
completionHandler(self.request!, self.response, responseJSON, response.result.error)
})
}
}
}
}
I'm not sure if this is compatible with older versions, but I suggest you rewrite your extensions as this:
extension DataRequest {
public func responseSwiftyJSON(queue: DispatchQueue? = nil, options: JSONSerialization.ReadingOptions = .allowFragments, completionHandler: #escaping (URLRequest?, HTTPURLResponse?, SwiftyJSON.JSON, Error?) -> Void) -> Self {
return responseJSON(queue: queue, options: options) {
let responseJSON: JSON
switch result {
case let .success(json): responseJSON = JSON(json)
case .failure: responseJSON = JSON.null
}
completionHandler(request, response, responseJSON, error)
}
}
}

How do I make task wait for completion in Swift 3

I'm using Alamofire to send a login request to an HTTP server. It returns a JSON response. My problem is that the mainline code finishes before the .responseJSON. How do I wait until the response is returned before returning from the function?
var ret: Bool = true
Alamofire.request(
URL(string: "http://localhost:8081/login/iPhone")!,
method: .post,
parameters: ["email":"test#test.test", "password":"test", "uuid":String(describing: UIDevice.current.identifierForVendor!.uuidString)],
headers: [:])
.validate()
.responseJSON{(response) -> Void in
do {
guard response.result.isSuccess else {
throw FieldError.fetchError(responseError: response.result.error)
}
guard let value = response.result.value as? [String: Any],
let status = value["status"] as? String,
let message = value["message"] as? String else {
throw FieldError.messageFormatError
}
switch status {
case "Login suggess":
break
default:
throw FieldError.fieldServerError(status: status, message: message)
}
} catch {
ret = false
debugPrint(error)
}
}
return ret
You can always use global
typealias DownloadComplete = () -> ()
next in func with you're json
func yourFunc(completed: #escaping DownloadComplete)
and after download use
completed()

Expression type 'DataRequest' is ambiguous without more context Swift 3

I am newbie and I have an error that I don't know how to fix, so I would appreciate all the helps. I am migrating from swift 2 to swift 3 and I get this error:
Expression type 'DataRequest' is ambiguous without more context
Here is my code:
static func renewToken(_ onSuccess: #escaping (JSON) -> Void, onFailure: #escaping (NSError) -> Void) {
let token = DataManager.token?.token
let header = ["Authorization": "Bearer "+token!]
Alamofire.request("\(BASE_URL)\(RENEWTOKEN_PATH)", method: .get, parameters: nil, encoding: .JSONEncoding.default, headers: header)
.validate()
.responseJSON { response in
switch response.result{
case .Success(let jsonObj):
onSuccess(JSON(jsonObj))
case .Failure(let error):
onFailure(error)
}
}
}
Your error is misleading you need to make 3 changes with your code.
With encoding its not .JSONEncoding.default but simply JSONEncoding.default
With Alamofire 4.* and Swift 3.* case .Success and .Failure of Result enum is now write in lowercase like .success and .failure.
From Swift 3 use Error instead of NSError.
So whole code goes like this.
static func renewToken(_ onSuccess: #escaping (JSON) -> Void, onFailure: #escaping (Error) -> Void) {
let token = ""
let header = ["Authorization": "Bearer "+token]
Alamofire.request("", method: .get, parameters: nil, encoding: JSONEncoding.default, headers: header)
.validate()
.responseJSON { response in
switch response.result{
case .success(let jsonObj):
onSuccess(JSON(jsonObj))
case .failure(let error):
onFailure(error)
}
}
}

Swift 3: Alamorefire block UI

I have APIManager singleton class and have a function to get data from server like this:
func scanOrder(order: String, completion:#escaping Handler){
let url = K.API_URL + "/api/containers/picking/" + order
Alamofire.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: getHeader()).responseJSON { (response) in
DispatchQueue.main.async {
completion(response)
}
}
}
and I call this function in other class like this:
apiMan.scanOrder(order: tfCode.text!) { (response) in
...
}
while waiting for server to response, my UI is blocked. I tried to wrap alamofire request call within DispatchQueue.global().async but it still blocks the UI.
Please help!
I never used Alamofire.request with DispatchQueue.main.async like you do. The reason is that Alamofire in combination with completion blocks already operates async and shouldn't block the UI, which is settled in the Main Thread.
Have you tried something like:
class NetworkManager {
func scanOrder(order: String, completion:#escaping (Any?) -> Void){
let url = "https://example.com/api/containers/picking/" + order
Alamofire.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: AppConfiguration.sharedInstance.defaultHeader())
.responseJSON { response in
guard response.result.isSuccess else {
Log.info("Error while fetching: \(response.result.error)")
completion(nil)
return
}
guard let responseJSON = response.result.value as? [String: AnyObject] else {
Log.info("Invalid information received from service")
completion(nil)
return
}
completion(responseJSON)
}
}
}
Call:
class CallingClass {
func scanOrder(order:String){
let manager = NetworkManager()
var result: Any?
manager.scanOrder(order: "example") { response in
result = response
}
print(result as Any)
}
}