title: How to programmatically go back to root view controller with a global function?
body: <p>I have 3 main VC (MyAccount, Register, Login). Then I have a Swift file(Manager.swift) where I set up a class function where after the registering or logging in it would dismiss the VC.</p>
  
  <p>Manager.swift</p>
  
  <pre><code>class Manager {
  
    class func registerUser(vc: UIViewController?, user_email : String, user_password : String){
      let myUrl = NSURL(string: hostURL + "userRegister.php")
      let request = NSMutableURLRequest(URL:myUrl!)
  
      request.HTTPMethod = "POST";
      // Compose a query string
      let postString = "user_email=\(user_email)&amp;user_password=\(user_password)";
  
      request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding);
  
      let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
          data, response, error in
  
          if error != nil
          {
              print("error=\(error)")
              return
          }
  
          // You can print out response object
          print("response = \(response)")
  
          // Print out response body
          let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
          print("responseString = \(responseString)")
  
          //Let’s convert response sent from a server side script to a NSDictionary object:
  
          let myJSON = try!NSJSONSerialization.JSONObjectWithData(data!, options: .MutableLeaves) as? NSDictionary
  
          if let parseJSON = myJSON {
  
              let status = parseJSON["status"] as? String
              let msg = parseJSON["message"] as? String
              print("Status: \(status)")
  
              if(status != "Success")
              {
              UIAlert.displayAlert(vc, title: "Error", message: msg!, dismissTxt: "OK")
              }
              else{
                  defaults.setBool(true, forKey: "isLoggedIn")
  
  //My Failed attempt to go back to Root :C                    
  vc?.navigationController?.popToRootViewControllerAnimated(true)
              }
          }
  
      }
  
      task.resume()
  
  }
  
  
  }
  </code></pre>
  
  <p>RegisterVC</p>
  
  <pre><code>buttonPressed(sender: AnyObject) {
  
    Manger.registerUser(self, user_email:username, user_password:password1234)
  
  }
  </code></pre>
  
  <p>I used navigationController?.popToRootViewControllerAnimated in an attempt to back
  It would work but sometimes the app would crash, spitting out this:</p>
  
  <blockquote>
    <p>2015-10-02 14:51:01.410 Retail_Template[4551:261846] <strong>* Assertion failure in -[UIKeyboardTaskQueue waitUntilAllTasksAreFinished], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3505.16/Keyboard/UIKeyboardTaskQueue.m:378
    2015-10-02 14:51:01.415 Retail_Template[4551:261846] *</strong> Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] may only be called from the main thread.'
    *** First throw call stack:
    (
        0   CoreFoundation                      0x009f8a94 __exceptionPreprocess + 180
        1   libobjc.A.dylib                     0x02b26e02 objc_exception_throw + 50
        2   CoreFoundation                      0x009f892a +[NSException raise:format:arguments:] + 138
        3   Foundation                          0x010bd3e6 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 118
        4   UIKit                               0x021f9d2e -[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] + 180
        5   UIKit                               0x019e14f2 -[UIKeyboardImpl setDelegate:force:] + 703
        6   UIKit                               0x019e122e -[UIKeyboardImpl setDelegate:] + 60
        7   UIKit                               0x01ddd1d6 -[UIPeripheralHost(UIKitInternal) _reloadInputViewsForResponder:] + 1208
        8   UIKit                               0x01de6bef -[UIPeripheralHost(UIKitInternal) _preserveInputViewsWithId:animated:reset:] + 502
        9   UIKit                               0x01de6c85 -[UIPeripheralHost(UIKitInternal) _preserveInputViewsWithId:animated:] + 57
        10  UIKit                               0x0190dd0a -[UINavigationController navigationTransitionView:didStartTransition:] + 1029
        11  UIKit                               0x01903bf4 -[UINavigationController _startCustomTransition:] + 5104
        12  UIKit                               0x01913c0b -[UINavigationController _startDeferredTransitionIfNeeded:] + 801
        13  UIKit                               0x01914d05 -[UINavigationController __viewWillLayoutSubviews] + 68
        14  UIKit                               0x01aded9f -[UILayoutContainerView layoutSubviews] + 252
        15  UIKit                               0x017cb16b -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 813
        16  libobjc.A.dylib                     0x02b3b059 -[NSObject performSelector:withObject:] + 70
        17  QuartzCore                          0x003ed60c -[CALayer layoutSublayers] + 144
        18  QuartzCore                          0x003e128e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 388
        19  QuartzCore                          0x003e10f2 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26
        20  QuartzCore                          0x003d3c2b _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 317
        21  QuartzCore                          0x00407c23 _ZN2CA11Transaction6commitEv + 589
        22  QuartzCore                          0x00407fbd _ZN2CA11Transaction14release_threadEPv + 289
        23  libsystem_pthread.dylib             0x04bc32f7 _pthread_tsd_cleanup + 93
        24  libsystem_pthread.dylib             0x04bc3051 _pthread_exit + 108
        25  libsystem_pthread.dylib             0x04bc3734 pthread_get_stackaddr_np + 0
        26  libsystem_pthread.dylib             0x04bc0e0e start_wqthread + 30
    )
    libc++abi.dylib: terminating with uncaught exception of type NSException</p>
  </blockquote>
  
  <p>Not sure what I'm doing wrong here.
  Any solution will be appreciated. Thanks!</p>