1

I'm creating an IOS app using swift. Recently, I've encountered a weird bug. Im trying to check if a url is valid, therefore, I'm creating a request with the url and check for the response. I do this task with dataTaskWithRequest of NSUrlSession. The weird bug is that if the URL is alibaba ,the response returns after a long time(more than 20 seconds sometimes). Why does that happen? As far as i concerned it happens only with this specific url. Here is some code although its not necessary .

let request = NSMutableURLRequest(URL: validatedUrl)
        request.HTTPMethod = "HEAD"

        let session = NSURLSession.sharedSession()

        let task = session.dataTaskWithRequest(request){ data, response, error in
            // The response here returns after a very long time
            let url = request.URL!.absoluteString

I would appreciate some help guys!

Eden.F
  • 219
  • 2
  • 6
  • This does not make sense. I check if there is not result, so the timeout is not good for me because it will cause the same response(and it will still have long time waiting which i dont want) In addition, it doesn't happen with other strings, so it cant be it. – Eden.F May 18 '16 at 20:54

1 Answers1

0

You're retrieving the contents of a URL over the Internet. The speed at which this happens is arbitrary. It depends on the speed of the DNS server that looks up the hostname, the speed of the web server that responds to the request, the speed of the user's Internet connection, and the speed of every network in between.

You can safely assume that it will either succeed or time out within three minutes. Twenty seconds isn't even slightly unusual over a cellular network.

You should probably rethink what you're doing with this URL and why you're doing it, or at least try to figure out a way to avoid keeping the user waiting while you fetch the URL.

dgatwood
  • 10,129
  • 1
  • 28
  • 49
  • Hey, This code is used for an implementation of a browser inside my app. The user enters any url, if the url is not found, it will redirect for a google search. This is the way i think i could that, do you have any idea how can i avoid this problem,maybe in a different approach? – Eden.F May 19 '16 at 19:35
  • That's probably the only valid reason to make the user wait. After all, the user requested the page, and they probably expect it to come up. If I were implementing it, I'd probably set about a five-second timer, and if you haven't received the initial response from the server (``...didReceiveResponse:...``), then try to figure out whether it is A. a temporary network glitch or B. a permanent issue. – dgatwood May 20 '16 at 18:19
  • Try simultaneously opening two additional data tasks—one for Google's generate_204 site and the second for the original URL. If the second attempt at the URL responds with a 200, cancel the original request and show the results. If the original request completes, cancel the additional requests and show the results. After five seconds, if neither of the additional requests has responded, cancel them, because the network is dead. – dgatwood May 20 '16 at 18:21
  • If generate_204 responds within that 5-second window, show a "Still waiting. The server is being slow" message and give the user a button to cancel and search instead. – dgatwood May 20 '16 at 18:22