swift 两个视图之间的转换需要很长时间

swift transition between two views take very long time

与 window.rootViewController 两个视图之间的转换大约需要 1 分钟

当 return 成功时,我可以连接到我的服务器,我更改了我的 rootViewControler,但这需要很长时间。如果我跳转连接代码,视图会立即出现吗?

    self.logingSpinner.startAnimating();

    var request = NSMutableURLRequest(URL: NSURL(string: "http://apps/appsAuth")!)
    var session = NSURLSession.sharedSession()
    request.HTTPMethod = "POST"
    var err: NSError?

    var bodyData="siren=\(entrepriseId.text)&email=\(email.text)&pwd=\(pwd.text)";

    request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding);

    request.HTTPMethod = "POST"
    request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")

    var task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in

        var strData = NSString(data: data, encoding: NSUTF8StringEncoding)

        var err: NSError?
        var json = NSJSONSerialization.JSONObjectWithData(data, options: .MutableLeaves, error: &err) as? NSDictionary


        if(err != nil) {
            println(err!.localizedDescription)
            let jsonStr = NSString(data: data, encoding: NSUTF8StringEncoding)
            println("Error could not parse JSON: '\(jsonStr)'")
        }
        else {

            if let parseJSON = json {
                println("data retour \(parseJSON)")

                var success = parseJSON["error"] as? Int
                println("Succes: \(success)")


                dispatch_async(dispatch_get_main_queue()) {
                    self.logingSpinner.stopAnimating();
                    bt.enabled = true;
                    bt.alpha = 1;
                }

                // si une erreur symfony est retournée
                if(success==nil){ success = 3};

                switch success!{
                    case 0:
                        println("auth success");
                       var data =  userDefault.dictionaryForKey("userInfos")!
                        data["login"] = self.email.text;
                        data["siren"] = self.entrepriseId.text;
                        data["pwd"] = self.pwd.text;
                        data["isLogged"] = true;
                        data["hasCredential"] = true;

                        userDefault.setObject(data, forKey : "userInfos");
                        userDefault.synchronize();
//CHANGE root View Controller 
                        let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
                        let initViewController: UIViewController = storyBoard.instantiateViewControllerWithIdentifier("viewAcc") as accueilViewController
                        var window :UIWindow = UIApplication.sharedApplication().keyWindow!
                        window.rootViewController = initViewController
                        window.makeKeyAndVisible()


                    case 1 :
                        println("auth failed")
                    default :
                        println("default");
                }


            }
            else {
                // Woa, okay the json object was nil, something went worng. Maybe the server isn't running?
                let jsonStr = NSString(data: data, encoding: NSUTF8StringEncoding)
                println("Error could not parse JSON: \(jsonStr)")
            }



        }
    })

    task.resume()

我已经尝试使用 presentViewControler,转换是即时的,但是我在视图中的图像消失了,只有按钮在位?
在此先感谢您的帮助

NSURLSession 负责减慢视图转换

我还没找到原因!但这是有罪的。这很奇怪,因为完成函数在最后。有人知道在完成函数之后,一个过程是否曾经 运行.
抱歉我的英语很糟糕:-(

 let initViewController: UIViewController = storyBoard.instantiateViewControllerWithIdentifier("viewAcc") as accueilViewController
                var window :UIWindow = UIApplication.sharedApplication().keyWindow!
 window.rootViewController = initViewController
 window.makeKeyAndVisible()
 println("end")

在控制台中执行 println("end"),我在控制台中看到来自我的 accueilviewcontroller viewDidAppear 函数的 println(viewdidload),但是视图在一分钟多后出现。

已解决

是的!经过漫长的一夜,我发现为什么我的视图在执行 wiewdidload 后需要很长时间才能显示。

是关于thread和NSUrlSession的,NSUrlSession的回调在后台线程,就像我在里面处理rootview一样,view不是在主线程处理的,应该是(apple在用户界面上的推荐)!! !!
为了解决它,我只是 return 在主线程上执行这段代码。在我的 completionHandler 中我做到了

var task = self.session.dataTaskWithRequest(self.request, completionHandler: {data, response, error -> Void in

        dispatch_async(dispatch_get_main_queue(),{

                mycallback(resultat: "success")

            })
 })

mycallback 函数句柄 rootviewcontroller
工作很棒.....