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

Does Alamofire actually work with scripts? #5

Open
mgrebenets opened this issue Jul 8, 2015 · 1 comment
Open

Does Alamofire actually work with scripts? #5

mgrebenets opened this issue Jul 8, 2015 · 1 comment

Comments

@mgrebenets
Copy link

I'm trying to make it work, using a basic sample, which is supposed to have JSON response.

Alamofire.request(.GET, "http://httpbin.org/get")
    .responseJSON { (_, _, JSON, _) in
        println("done")
        println(JSON)
    }

println("last")

When I run the script, it just exits and print nothing from network request, only the "last" bit is printed.

I suspect that's due to async behavior of Alamofire. Do I have to do additional synchronization myself to make sure the script doesn't exit before n/w response gets back?

@marcoconti83
Copy link

In scripts, you (most likely) want to turn the asynchronous behavior in synchronous behavior. I.e. you want the request to complete before the next line of code is executed.
However, you should not block the main run loop, or the request will never complete (at least in my experience).
I found a working solution using something like this:

var done = false
Alamofire.request(.GET, "http://httpbin.org/get")
    .responseJSON { (_, _, JSON, _) in
        println("done")
        println(JSON)
        done = true
    }

while !done {
    NSRunLoop.currentRunLoop().runUntilDate(NSDate(timeIntervalSinceNow: 0.5))
}
println("last")

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