title: Swift - Crashing With Reachability
body: <p>I call the method testReachability() when a UIButton is pressed to check for internet connection before it completes the action. This is using Ashley Mill's reachability class.</p>
  
  <p>On Line 24 I call completed() so that if the app can reach the network then it will complete the closure and continue on with the rest of the action. However the app becomes glitchy and this error displays in the debug console:</p>
  
  <p>"This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes.  This will cause an exception in a future release.”</p>
  
  <p>And If I call completed() on lets say, line 26, then the app runs flawlessly, quick and smooth until I lose internet connection/enter airplane mode. Then the app crashes instantly and xcode loses connection to the device on the tap of the button.</p>
  
  <p>My question is what can I do to resolve the crashing of my application when checking for reachability.</p>
  
  <pre><code>func testReachability(completed: DownloadComplete)
  {
      let reachability: Reachability
      do {
          reachability = try Reachability.reachabilityForInternetConnection()
      } catch {
          return
      }
  
      reachability.whenReachable = { reachability in
          // this is called on a background thread, but UI updates must
          // be on the main thread, like this:
          dispatch_async(dispatch_get_main_queue())
          {
              if reachability.isReachableViaWiFi()
              {
                  print("Reachable via WiFi")
              }
              else
              {
                  print("Reachable via Cellular")
              }
          }
          completed()
      }
  
      reachability.whenUnreachable = { reachability in
          // this is called on a background thread, but UI updates must
          // be on the main thread, like this:
          dispatch_async(dispatch_get_main_queue())
          {
             self.presentReachabilityAlertController()
          }
      }
  
      do
      {
          try reachability.startNotifier()
      }
      catch
      {
  
      }
  }
  
  func presentReachabilityAlertController()
  {
      let alert = UIAlertController( title: "Network Connection Lost", message:"Try Again", preferredStyle: .Alert)
      alert.addAction(UIAlertAction( title: "ok"  , style: .Default) { _ in } )
      presentViewController        ( alert, animated: true                   ) {      }
      connected = false
  }
  </code></pre>