xcode7 - Fixing NSURLConnection Deprecation from Swift 1.2 to 2.0 -


i have function written in swift 1.2, checks reachability of address or ip. here :

func ishostconnected(hostaddress : string) -> bool {     var response : nsurlresponse?     let request = nsmutableurlrequest(url: nsurl(string: hostaddress)!)     request.timeoutinterval = 3      let data = nsurlconnection.sendsynchronousrequest(request, returningresponse: &response, error: nil)      return ((response as? nshttpurlresponse)!.statuscode == 200) } 

now since, nsurlconnection deprecated, per xcode suggestion tried writing using nsurlsession.datataskwithrequest, here :

func ishostconnected(hostaddress : string) -> bool {     let request = nsmutableurlrequest(url: nsurl(string: hostaddress.stringbyaddingpercentescapesusingencoding(nsutf8stringencoding)!)!)     request.timeoutinterval = 3      let session = nsurlsession(configuration: nsurlsessionconfiguration.defaultsessionconfiguration(), delegate: nil, delegatequeue: nsoperationqueue.mainqueue())      var responsecode = -1      session.datataskwithrequest(request, completionhandler: {(data : nsdata?, response : nsurlresponse?, error : nserror?) -> void in         responsecode = (response as? nshttpurlresponse)!.statuscode     })!.resume()      return (responsecode == 200) } 

above code returns false (its obvious), since completionhandler gets executed on seperate thread.

my concern that, can make completionhandler() run on mainthread somehow sendsynchronousrequest blocking it.

i have reasons not use 'apple's reachabilty' here.

any suggestion helpful. :)

(repeating arguments https://stackoverflow.com/a/30670518/1187415:)

checking if resource exists on server requires sending http request , receiving response. tcp communication can take amount of time, e.g. if server busy, router between client , server not work correctly, network down etc.

that's why asynchronous requests preferred. if think request should take milliseconds, might seconds due network problems. , – know – blocking main thread seconds big no-no.

that being said, can use "counting semaphore" or "dispatch group" wait completion of asynchronous task. should not use on main thread. blocking main thread 3 seconds not acceptable!

func ishostconnected(hostaddress : string) -> bool {     let request = nsmutableurlrequest(url: nsurl(string: hostaddress.stringbyaddingpercentescapesusingencoding(nsutf8stringencoding)!)!)     request.timeoutinterval = 3     request.httpmethod = "head"      let session = nsurlsession(configuration: nsurlsessionconfiguration.defaultsessionconfiguration())     var responsecode = -1      let group = dispatch_group_create()     dispatch_group_enter(group)      session.datataskwithrequest(request, completionhandler: {(_, response, _) in         if let httpresponse = response as? nshttpurlresponse {             responsecode = httpresponse.statuscode         }         dispatch_group_leave(group)     })!.resume()      dispatch_group_wait(group, dispatch_time_forever)     return (responsecode == 200) } 

remarks:

  • setting http method "head" small optimization, server sends response header without actual data.
  • in case of illegal host name, response nil, , responsecode = (response as? nshttpurlresponse)!.statuscode crash.

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -