I'm working on a small chunk of Swift code to work with pulling data via web-based APIs. Right now I am close, but think I'm missing the completion handler aspect when I print the data within the getUserInfo() expected data is there, but outside that function, the initialized default data appears. The function is called like this:
print("Provided Username is: \(workingData.user)")
getUserInfo()
print("Returned String Data is: \(workingData.responseDataString)")
and the actual function:
func getUserInfo() {
Alamofire.request(workingjss.jssURL + devAPIMatchPath + workingData.user, method: .get)
.authenticate(user: workingjss.jssUsername, password: workingjss.jssPassword).responseString { response in
if (response.result.isSuccess) {
print("In Function Data: \(response.result.value!)"
workingData.responseDataString = response.result.value!
}
}
}
The output in running the code is:
Provided Username is: MYUSER
Returned String Data is: Nothing Here Yet
In Function Data: {"Cleaned JSON Data here"}
Would a completion handler help the issue out? I'm pretty new to working with Alamofire so sorry if this is an easy one. Thanks!
Try using a completion handler:
func getUserInfo(completion: #escaping (String) -> Void) {
Alamofire.request(workingjss.jssURL + devAPIMatchPath + workingData.user, method: .get)
.authenticate(user: workingjss.jssUsername, password: workingjss.jssPassword).responseString { response in
if (response.result.isSuccess) {
print("In Function Data: \(response.result.value!)"
completion(response.result.value!)
}
}
}
And call it like:
getUserInfo() { response in
// Do your stuff here
workingData.responseDataString = response
print("Returned String Data is: \(workingData.responseDataString)")
}
Related
Hello new to Swift and Alamofire,
The issue i'm having is when I call this fetchAllUsers() the code will return the empty users array and after it's done executing it will go inside the AF.request closure and execute the rest.
I've done some research and I was wondering is this is caused by Alamofire being an Async function.
Any suggestions?
func fetchAllUsers() -> [User] {
var users = [User]()
let allUsersUrl = baseUrl + "users/"
if let url = URL(string: allUsersUrl) {
AF.request(url).response { response in
if let data = response.data {
users = self.parse(json: data)
}
}
}
return users
}
You need to handle the asynchrony in some way. This this means passing a completion handler for the types you need. Other times it means you wrap it in other async structures, like promises or a publisher (which Alamofire also provides).
In you case, I'd suggest making your User type Decodable and allow Alamofire to do the decoding for you.
func fetchAllUsers(completionHandler: #escaping ([User]) -> Void) {
let allUsersUrl = baseUrl + "users/"
if let url = URL(string: allUsersUrl) {
AF.request(url).responseDecodable(of: [User].self) { response in
if let users = response.value {
completionHandler(users)
}
}
}
}
However, I would suggest returning the full Result from the response rather than just the [User] value, otherwise you'll miss any errors that occur.
In Vapor 4, I'm processing a post request by calling a request on a 3rd party API and returning a value based on the result I get back. The following code results in the error: "Invalid conversion from throwing function ... to non-throwing function"
app.post("activate") { req -> EventLoopFuture<ActivationRequestResponse> in
return req.client.post("https://api.example.com/activation", headers: HTTPHeaders(), beforeSend: { (req) in
try req.content.encode(RequestBody(value: someValue), as: .json)
})
.map { (response) -> ActivationRequestResponse in
let response = try response.content.decode(ResponseModel.self)
return ActivationRequestResponse(success: true, message: "success")
}
}
I can't seem to use try in my chained map() after getting the API result. The above code will work if I add a ! to the try in let response = try response.content.decode(ResponseModel.self) inside the map, but ideally I want to catch this error. The first try used when creating the response body seems to be implicitly passed back up the chain, but not the second.
What am I doing wrong? How do I catch the error when decoding the response content? Why is the first try caught but not the second?
The property of map is that it will just transform a value on the “success path”. Your transformation may however fail which means that you presumably want the future to fail too.
Whenever you want to transform a value with a function that either succeeds or fails you need to use one of the flatMap* functions.
In your case, try replacing map with flatMapThrowing and then it should work.
To expand on Johannes Weiss' answer, to have a throwing closure that returns a future, you need something like:
future.flatMap {
do {
return try liveDangerously()
} catch {
future.eventLoop.makeFailedFuture(error)
}
}
After doing this too many times, I decided to roll my own (though the name is a bit dubious):
extension EventLoopFuture {
#inlinable
public func flatterMapThrowing<NewValue>(file: StaticString = #file,
line: UInt = #line,
_ callback: #escaping (Value) throws -> EventLoopFuture<NewValue>) -> EventLoopFuture<NewValue> {
return self.flatMap(file: file, line: line) { (value: Value) -> EventLoopFuture<NewValue> in
do {
return try callback(value)
} catch {
return self.eventLoop.makeFailedFuture(error)
}
}
}
}
That way you can just write:
future.flatterMapThrowing {
return try liveDangerously()
}
The code within the function is executed in a different order than it is expected. I wanted to change the state of the login Boolean variable inside the if statement, but the function returns the initial value before if statement is completed.
Code sample:
class ClassName {
func loginRequest (name: String, pwd: String) -> Bool {
var login:Bool
//Initial value for login
login = false
let task = session.uploadTask(with: request, from: jsonData) { data, response, error in
if let httpResponse = response as? HTTPURLResponse {
print(httpResponse.statusCode)
if (httpResponse.statusCode) == 200 {
//Change the value of login if login is successful
login = true
if let data = data, let dataString = String(data: data, encoding: .utf8) {
do {
...
} catch {print(error.localizedDescription)}
}
}
}
}
task.resume()
//Problem return false in any case because return is completed before if statement
return login
}
}
Completion Handlers is your friend
The moment your code runs task.resume(), it will run your uploadTask and only when that function is finished running it will run the code where you change your login variable.
With That said: That piece of code is running asynchronously. That means your return login line of code won't wait for your network request to come back before it runs.
Your code is actually running in the order it should. But i myself wrote my first network call like that and had the same problem. Completion Handles is how i fixed it
Here is a very nice tutorial on Completion Handlers or you might know it as Callbacks :
Link To Completion Handlers Tutorial
If i can give you a little hint - You will have to change your function so it looks something like this: func loginRequest (name: String, pwd: String, completionHandler: #escaping (Bool) -> Void)
And replace this login = true with completionHandler(true)
Wherever it is you call your function it will look something like this:
loginRequest(name: String, pwd: String) {didLogIn in
print("Logged In : \(didLogIn)")
}
One last thing... You're actually already using Completion Handlers in your code.
let task = session.uploadTask(with: request, from: jsonData) { data, response, error in
... ... But hopefully now you understand a little bit better, and will use a completion handler approach when making network calls.
GOOD LUCK !
I just began learning Swift, I am working with an API behind, the problem is that : Swift don't wait my function finish therefore my last function appears like no code was done before.
import Foundation
import UIKit
import Alamofire
import SwiftyJSON
// Classes
class User {
init(data: Any) {
self.sex = JSON(data)["sex"].string!
print(self.sex)
}
var id: Int = 1
var online: Bool = false
var picture: String = ""
var sex: String = "Male"
}
// Fonctions
func getBackground(_ apiURL: String, completion : #escaping(_ :Any) -> Void) {
// Requête API avec Alamofire + SwiftyJSON
AF.request(apiURL, method: .get).validate().responseJSON { response in
switch response.result {
case .success(let value):
let jsonData = JSON(value)
completion(jsonData["results"])
case .failure(let error):
print(error)
}
}
}
// Requête de connexion
getBackground("https://x..me", completion: { response in
let user = User(data: response)
})
print(String(user.id) + ": " + String(user.online!))
Screenshot here
I have this error: "Use of unresolved identifier 'user'", I guess that Swift don't get the fact that User was defined previously
All my work works perfectly with my API, the "self.sex" is set and showed when I build the code.
But I'm still "locked" like I can't do nothing after this code because Swift don't want to wait.
I tried the function async and sync but then, all my next code has to be under a function...
Thanks in advance
There's no need to wait.
Put the print line – and the code which proceeds the user – in the completion closure
getBackground("https://a.clet.me", completion: { response in
let user = User(data: response)
print(String(user.id) + ": " + String(user.online!))
})
The error Use of unresolved identifier 'user' occurs because the local variable user is only visible inside the scope ({}) of its declaration.
So, this problem is all about variables life cycle.
// This happens first
getBackground("https://a.clet.me", completion: { response in
let user = User(data: response)
// This happens third, once the request has completed.
// user exists here.
// Only after this moment you should handle anything related to the user
})
// This happens second
// user DOESN'T exist here, swift will move on
I'm having troubles with getting password salt from my VM server via Alamofire. I'm making a request to server and it should return me salt, so I can salt my password, hash it and send it back to server.
The problem is that I do not understand how to save the salt, that Alamofire receives, into a variable, so I can just add it to password and hash it like that:
let salted_password = user_password + salt
let hash = salted_password.sha1()
Where user_password is what the user entered into password field and salt is what I got from Alamofire salt request.
Here is my code:
func getSalt(completionHandler: #escaping (DataResponse<String>, Error?) -> Void) {
Alamofire.request("http://192.168.0.201/salt", method: .post, parameters: salt_parameters).responseString { response in
switch response.result {
case .success(let value):
completionHandler(response as DataResponse<String>, nil)
case .failure(let error):
completionHandler("Failure", error)
}
}
}
let salt = getSalt { response, responseError in
return response.result.value!
}
It gives me the following error:
Binary operator '+' cannot be applied to operands of type 'String' and '()'.
So is it possible to save a request value into a variable? What should I do?
Thank you for your attention.
The problem here is because of how you implemented your completion block
For example:
func someAsynchronousCall(a: Int, b: Int, #escaping block: (_ result: Int) -> Void) {
... some code here
... {
// let's just say some async call was done and this code is called after the call was done
block(a + b)
}
}
To use this code it would look like this:
var answer: Int = 0
someAsynchronousCall(100, b: 200) { result in // the `result` is like what you're returning to the user since API calls need to be called asynchronously you do it like this rather than creating a function that has a default return type.
answer = result
print(answer)
}
print(answer)
The print would look like this
0
300
Since we declared answer as 0 it printed that first since the async call wasn't done yet, after the async call was done (usually a few milliseconds after) it then printed 300
So all in all your code should look something like this
var salt: String?
getSalt { response, responseError in
salt = response.result.value
}