ios - Swift - Crashing With Reachability -


i call method testreachability() when uibutton pressed check internet connection before completes action. using ashley mill's reachability class.

on line 24 call completed() if app can reach network complete closure , continue on rest of action. app becomes glitchy , error displays in debug console:

"this application modifying autolayout engine background thread, can lead engine corruption , weird crashes. cause exception in future release.”

and if call completed() on lets say, line 26, app runs flawlessly, quick , smooth until lose internet connection/enter airplane mode. app crashes instantly , xcode loses connection device on tap of button.

my question can resolve crashing of application when checking reachability.

func testreachability(completed: downloadcomplete) {     let reachability: reachability     {         reachability = try reachability.reachabilityforinternetconnection()     } catch {         return     }      reachability.whenreachable = { reachability in         // called on background thread, ui updates must         // on main thread, this:         dispatch_async(dispatch_get_main_queue())         {             if reachability.isreachableviawifi()             {                 print("reachable via wifi")             }             else             {                 print("reachable via cellular")             }         }         completed()     }      reachability.whenunreachable = { reachability in         // called on background thread, ui updates must         // on main thread, this:         dispatch_async(dispatch_get_main_queue())         {            self.presentreachabilityalertcontroller()         }     }          {         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 } 

it sounds call complete needs execute on main thread. when inside dispatch_async(dispatch_get_main_queue()). try putting call complete inside dispatch_async follows:

dispatch_async(dispatch_get_main_queue()) {     if reachability.isreachableviawifi()     {         print("reachable via wifi")     }     else     {         print("reachable via cellular")     }     complete()    } 

if doesn't fix scenario when in airplane mode/offline think going need more code figure out. if show how/when function called, complete callbacks, , maybe presentreachabilityalertcontroller function helpful.


Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -