Ios Swift-函数不';不要等待结果
我一直在试图从服务器呼叫中获取信息。这已经很好用了,但是当我想用这些信息来显示一个对话框时,它并没有真正起作用。它到下一行太快了。我能让它工作的唯一方法是延迟代码中的下一位,但考虑到这是一个服务器调用,我无法确定它总是需要多长时间。我尝试使用dispatch_sync,但根本不起作用Ios Swift-函数不';不要等待结果,ios,xcode,swift,Ios,Xcode,Swift,我一直在试图从服务器呼叫中获取信息。这已经很好用了,但是当我想用这些信息来显示一个对话框时,它并没有真正起作用。它到下一行太快了。我能让它工作的唯一方法是延迟代码中的下一位,但考虑到这是一个服务器调用,我无法确定它总是需要多长时间。我尝试使用dispatch_sync,但根本不起作用 func logIn(username: String, password: String) -> Void { var error: NSError? var call = "api/use
func logIn(username: String, password: String) -> Void {
var error: NSError?
var call = "api/usersession/"
var login = "username=" + username + "&password=" + password
API().getLogin(login, api:call, { (data) -> Void in
let json = JSON(data: data, error: &error)
println(json.self)
let status = json["status"].stringValue!
if (status == "1") {
alertController = UIAlertController(title: "Something went wrong", message: json["result"].stringValue, preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
} else {
alertController = UIAlertController(title: "Welcome!", message: "You're signed in", preferredStyle: UIAlertControllerStyle.Alert)
}
})
}
我相信,每当代码到达“getLogin”部分时,它就会立即返回它所拥有的任何东西(这永远是空的)。当我说self.presentViewController需要延迟一定量时,它确实可以工作,但这会让应用程序感觉有点笨重
编辑:
和httpRequest:
func httpRequest(request: NSURLRequest!, callback: (NSData?,
String?) -> Void) {
var configuration =
NSURLSessionConfiguration.defaultSessionConfiguration()
var userPassWordString = apiusername + ":" + apipassword
let userPasswordData = userPassWordString.dataUsingEncoding(NSUTF8StringEncoding)
let base64EncodedCrendtial = userPasswordData!.base64EncodedStringWithOptions(nil)
let authString = "Basic \(base64EncodedCrendtial)"
configuration.HTTPAdditionalHeaders = ["Authorization": authString]
var session = NSURLSession(configuration: configuration,
delegate: self,
delegateQueue:NSOperationQueue.mainQueue())
var task = session.dataTaskWithRequest(request){
(data: NSData!, response: NSURLResponse!, error: NSError!) -> Void in
if error != nil {
callback(nil, error.localizedDescription)
} else {
var result = NSData(data: data)
callback(result, nil)
}
}
task.resume()
}
它到下一行太快了
您需要考虑异步操作。当你做一些需要花费时间的事情时,无论是向服务器发出网络请求还是要求用户提供一些信息,你都不能只进行函数调用,期望结果立即显示出来。相反,您可以调用启动流程的函数或方法,然后继续执行其他操作,直到流程完成。对于网络请求,这通常意味着您提供一个委托对象或一个完成块,当连接有新信息时将调用该委托对象或完成块。当与用户打交道时,您可能会弹出一个对话框,让它保持不变——当用户点击某个按钮或填写某个字段触发某个操作时,该过程将继续
它到下一行太快了
您需要考虑异步操作。当你做一些需要花费时间的事情时,无论是向服务器发出网络请求还是要求用户提供一些信息,你都不能只进行函数调用,期望结果立即显示出来。相反,您可以调用启动流程的函数或方法,然后继续执行其他操作,直到流程完成。对于网络请求,这通常意味着您提供一个委托对象或一个完成块,当连接有新信息时将调用该委托对象或完成块。当与用户打交道时,您可能会弹出一个对话框,并将其保留在该对话框中——当用户通过点击某个按钮或填写某个字段触发某个操作时,该过程将继续进行。我通过使UIAlertController从函数本身出现来修复它,如下所示:
if (status == "1") {
alertController = UIAlertController(title: "Er ging iets fout", message: json["result"].stringValue, preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(alertController,animated: true, completion:nil)
} else {
alertController = UIAlertController(title: "Welcome!", message: "You're signed in!", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(alertController,animated: true, completion:nil)
}
我通过使UIAlertController从函数本身显示出来,从而修复了它,如下所示:
if (status == "1") {
alertController = UIAlertController(title: "Er ging iets fout", message: json["result"].stringValue, preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(alertController,animated: true, completion:nil)
} else {
alertController = UIAlertController(title: "Welcome!", message: "You're signed in!", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(alertController,animated: true, completion:nil)
}
你能发布getLogin方法,让我们看看它的功能吗?这里没有足够的信息。你能发布getLogin方法吗?这样我们就可以看到它的作用了?这里没有足够的信息。尝试实现它,可能会因为无法使其工作而出错。尝试实现它,可能会因为无法使其工作而出错。