Reputation: 333
I have a connection to my server when the return is a success, I change my rootViewControler, but it take a very long time. if i jump the code for the connection the view appear immediately ?
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()
I have tried with presentViewControler the transition is immediat, but my image inside the view disapear , only the button are in place ?
thank in advance for help
Upvotes: 0
Views: 767
Reputation: 333
Yes ! after a long night i have find why my view take too much time to appear after the wiewdidload was executed.
it's about thread and NSUrlSession, with NSUrlSession the callback is on the background thread , and like I handle rootview inside, the view is not handled on the main thread, it should be (apple recommandation on user interface)!!!!
to solve it, I simply return this code execution on the main thread. in my completionHandler i do it
var task = self.session.dataTaskWithRequest(self.request, completionHandler: {data, response, error -> Void in
dispatch_async(dispatch_get_main_queue(),{
mycallback(resultat: "success")
})
})
mycallback function handle rootviewcontroller
work's great .....
Upvotes: 1
Reputation: 333
I have not find yet why ! but it's the guilty. it strange because the completion function go at the end. someone know if after the completion func, a process is ever running.
sorry for my very bad english :-(
let initViewController: UIViewController = storyBoard.instantiateViewControllerWithIdentifier("viewAcc") as accueilViewController
var window :UIWindow = UIApplication.sharedApplication().keyWindow!
window.rootViewController = initViewController
window.makeKeyAndVisible()
println("end")
in the console println("end") is execute, i see the println(viewdidload) from my accueilviewcontroller viewDidAppear function, in the console , but the view appear after more than one minute.
Upvotes: 1