Swift: Async callback in command line utility

315 views Asked by At

I'm trying to fetch data from a website via a command line utility in swift. I'm using NSURLConnection for this. Like so:

private let queue = NSOperationQueue.mainQueue()

NSURLConnection.sendAsynchronousRequest(NSURLRequest(URL: url), queue: queue) { (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in
        println("Hello")
}

But the application stops running before the callback is called. How do I keep the application running so it won't exit early? I found CFRunLoop() and have added that in my main file. Now the application doesn't terminate. But I still don't get a response.

The same project in a playground with XCPSetExecutionShouldContinueIndefinitely does work however. So the problem does not lie in the NSURLConnection code.

0

There are 0 answers