5

As the questions states, what's the most efficient way of checking to see if a website link is valid?

Currently I'm executing the following code, but when applied to a list of 20 URL's, it takes incredibly long to complete.

if let data = NSData(contentsOfURL: NSURL(string: thisURL)!) {
    println(thisURL)
} else {
    println("doesn't exist")
    break
}
Raymond Rangel
  • 149
  • 3
  • 12
  • If it is an HTTP URL you can use HEAD request method to quickly check if a link is valid. Frankly, I am not familiar with NSURL usage but see if this [link](http://sutes.co.uk/2009/12/nsurlconnection-using-head-met.html) helps simulate HEAD request using NSURL.Along with that, since network calls always incur some delay you can consider distributing your URL requests among multiple threads if that is feasible. – Madhusudana Reddy Sunnapu Dec 25 '15 at 03:43
  • 1
    This answer solves your problem: http://stackoverflow.com/a/29050673/1079908 – Ferit Dec 25 '15 at 03:44

1 Answers1

0

The solution above only tells you if you have a valid Internet connection - assuming Google.com is available. if you want to check the availability of a group of websites, you will need to check each one individually - and then you're stuck with the timeout constraints.

One solution is to launch 20 background tasks to try to access each website at the same time, and build up the overall answer as the results come in.

Depending on your application, it may be valid to do the whole block of 20 in a single background task - if you can start that early enough in your process, it may not matter that it takes a long time - only that it doesn't appear to hold up the user.

Russell
  • 5,436
  • 2
  • 19
  • 27