嗨,在将用户“用户名”保存在内部数据库中后,我正在使用以下功能从当前的根视图控制器交换到新的根视图控制器。虽然代码有效并且我可以进行切换,但转换需要很长时间。有时长达 15 秒。
func switchRootViewController(rootViewController: UIViewController, animated: Bool, completion: (() -> Void)?) {
if animated {
UIView.transitionWithView(self.window!, duration: 0.5, options: .TransitionCrossDissolve, animations: {
let oldState: Bool = UIView.areAnimationsEnabled()
UIView.setAnimationsEnabled(false)
self.window!.rootViewController = rootViewController
UIView.setAnimationsEnabled(oldState)
}, completion: { (finished: Bool) -> () in
if (completion != nil) {
completion!()
}
})
} else {
self.window!.rootViewController = rootViewController
}
}
这就是我调用函数的方式:
api.postMulti(apiKey, contentType: "application/json", payLoad: payLoad, urlString: urlString, parameter: parameter){ (succeeded: Int, msg: String) -> () in
var alert = UIAlertView(title: "Failed", message: msg, delegate: nil, cancelButtonTitle: "Okay")
if succeeded == 422 {
alert.title = "Failed"
alert.message = "Username is already in use. Please selecet another one!"
}
else if succeeded == 500{
alert.title = "Failed"
alert.message = "Internal Server Error. Unable to process Request!"
}
else if succeeded == 200{
//save to internal DataBase table: User
let entityDescription = NSEntityDescription.entityForName("User", inManagedObjectContext: self.managedObjectContext!)
let users = User(entity: entityDescription!, insertIntoManagedObjectContext: self.managedObjectContext)
users.username = self.userName.text
var error: NSError?
self.managedObjectContext?.save(&error)
if let err = error {
println(err.localizedFailureReason)
} else {
self.userName.text = ""
alert.title = "Success"
alert.message = "Loading App..."
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var secondViewController: EveryTimeRun = mainStoryboard.instantiateViewControllerWithIdentifier("everytime") as! EveryTimeRun
self.switchRootViewController(secondViewController, animated: true, completion: nil)
self.window?.makeKeyAndVisible()
}
}
// Move to the UI thread
dispatch_async(dispatch_get_main_queue(), { () -> Void in
// Show the alert
alert.show()
})
}
我犯了一个错误吗。
更新:
我正在发布我的 POST 功能。这可能需要很长时间:
func postMulti(apikey: String, contentType: String, payLoad: Dictionary<String,Dictionary<String,String>>, urlString: String, parameter: String, postCompleted : (succeeded: Int, msg: String) -> ()){
let joinedUrl = urlString + parameter
let url = NSURL(string: joinedUrl)
let request = NSMutableURLRequest(URL: url!)
var session = NSURLSession.sharedSession()
request.HTTPMethod = "POST"
request.addValue(contentType, forHTTPHeaderField: "Content-Type")
request.addValue(apikey, forHTTPHeaderField: "apikey")
var error: NSError?
let payLoadJSON = NSJSONSerialization.dataWithJSONObject(payLoad, options: nil, error: &error)
request.HTTPBody = payLoadJSON
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
// Did the JSONObjectWithData constructor return an error? If so, log the error to the console
if(err != nil) {
// println(err!.localizedDescription)
let jsonStr = NSString(data: data, encoding: NSUTF8StringEncoding)
postCompleted(succeeded: 0, msg: "Portal is not reachable. Please try again later")
}
else {
// The JSONObjectWithData constructor didn't return an error. But, we should still
// check and make sure that json has a value using optional binding.
if let parseJSON = json {
// Okay, the parsedJSON is here, let's get the value for 'success' out of it
if let statusCode = parseJSON["statusCode"] as? Int {
postCompleted(succeeded: statusCode, msg: "")
}
}
else {
// Woa, okay the json object was nil, something went worng. Maybe the server isn't running?
let jsonStr = NSString(data: data, encoding: NSUTF8StringEncoding)
postCompleted(succeeded: 1, msg: "Portal is not reachable. Please try again later")
}
}
})
task.resume()
}