-
Notifications
You must be signed in to change notification settings - Fork 313
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
Question about async #30
Comments
This isn't an issue with SwiftHTTP directly, but rather an issue with understanding async code design. I would recommend checking out these links: They both explain how GCD works as well as how closures work, both which should help in understanding how to structure async code. Also just off hand: func foo(username: String, password: String, finished:((Bool) -> Void)) -> Void {
var url = "http://example"
var request = HTTPTask()
var parameters = ["username": username, "password": password]
request.requestSerializer = JSONRequestSerializer()
request.POST(url, parameters: parameters,
success: {
(response: HTTPResponse) in
if response.responseObject != nil {
finished(true)
},
}, failure: {(error: NSError) in
println(" error \(error)")
finished(false)
})
} Then calling foo would look like: foo("test", password: "test", finished: {(result: Bool) -> Void in
println("result is: \(result)")
//do more stuff now that I know the result
}) |
Thanks @daltoniam var result: Bool = false
foo(username.text, password: password.text, finished: {(result:Bool) -> Void in
println("result is: \(result)")
}) |
ah good catch, I change it to be proper. Thanks. |
I'm trying to do a login and get the cookies from server.
Since its running async, my
result
always is false. Any suggestion of fixing it? Thanks.The text was updated successfully, but these errors were encountered: