Skip to content
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

Closed
ldong opened this issue Nov 4, 2014 · 3 comments
Closed

Question about async #30

ldong opened this issue Nov 4, 2014 · 3 comments

Comments

@ldong
Copy link

ldong commented Nov 4, 2014

I'm trying to do a login and get the cookies from server.

func foo(username: String, password: String) -> Bool {
        var url = "http://example"
        var request = HTTPTask()
        var parameters = ["username": username, "password": password]
        var result = false;
        request.requestSerializer = JSONRequestSerializer()
        request.POST(url, parameters: parameters,
        success: {
            (response: HTTPResponse) in
            if response.responseObject != nil {
                result = true
            },
        }, failure: {(error: NSError) in
                println(" error \(error)")
        })
    return result;
}

Since its running async, my result always is false. Any suggestion of fixing it? Thanks.

@daltoniam
Copy link
Owner

This isn't an issue with SwiftHTTP directly, but rather an issue with understanding async code design. I would recommend checking out these links:

https://developer.apple.com/library/mac/documentation/Performance/Reference/GCD_libdispatch_Ref/index.html

https://developer.apple.com/library/mac/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html

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
})

@ldong
Copy link
Author

ldong commented Nov 11, 2014

Thanks @daltoniam
I just want to point out the correct syntax for calling the function should be the following.

var result: Bool = false
foo(username.text, password: password.text, finished: {(result:Bool) -> Void in
        println("result is: \(result)")
})

@daltoniam
Copy link
Owner

ah good catch, I change it to be proper. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants