Synchronous URL request on Swift 2

前端 未结 4 1582
难免孤独
难免孤独 2020-11-29 09:43

I have this code from here to do synchronous request of a URL on Swift 2.

  func send(url: String, f: (String)-> ()) {
    var request = NSURLRequest(URL:         


        
4条回答
  •  北荒
    北荒 (楼主)
    2020-11-29 10:06

    There is a reason behind deprecation - there is just no use for it. You should avoid synchronous network requests as a plague. It has two main problems and only one advantage (it is easy to use.. but isn't async as well?):

    • The request blocks your UI if not called from different thread, but if you do that, why don't use asynchronous handler right away?
    • There is no way how to cancel that request except when it errors on its own

    Instead of this, just use asynchronous request:

    NSURLConnection.sendAsynchronousRequest(request, queue: queue, completionHandler:{ (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in
    
        // Handle incoming data like you would in synchronous request
        var reply = NSString(data: data, encoding: NSUTF8StringEncoding)
        f(reply)
    })
    

    iOS9 Deprecation

    Since in iOS9 this method is being deprecated, I suggest you to use NSURLSession instead:

    let session = NSURLSession.sharedSession()
    session.dataTaskWithRequest(request) { (data, response, error) -> Void in
    
        // Handle incoming data like you would in synchronous request
        var reply = NSString(data: data, encoding: NSUTF8StringEncoding)
        f(reply)
    }
    

提交回复
热议问题