viewcontroller中的标签填充速度非常慢

时间:2017-02-28 17:42:58

标签: ios json swift3 uilabel populate

我在视图控制器中极度缓慢地使用标签。

该脚本将用户标识发送到php脚本。脚本检索与数组中的userid相关的数据,并将其作为json响应发送回我的应用程序。然后,我的viewcontroller中的标签将填充json数组(字典)中的相应数据。

“do {}”中的print语句立即在控制台中执行,但viewcontroller中标签的填充需要很长时间。将self.lblStreet.text = self.loadedStreet放在do {}之外,但仍然在任务中,它会产生相同的缓慢结果。

class CharDetailViewController: UIViewController {

//Outlets
@IBOutlet weak var lblUID: UILabel!
@IBOutlet weak var lblStreet: UILabel!

//URL to our web service
let URL_GET_DETAILS = "http://somesite.com/script.php"

var passedUID: String!
var loadedStreet: String!

override func viewDidLoad() {
    super.viewDidLoad()

    let url = URL(string: URL_GET_DETAILS)
    lblUID.text = passedUID

    let request = NSMutableURLRequest(url: url! as URL)
    request.httpMethod = "POST";
    let sendUID = lblUID.text
    let postParameters = "sendUID="+sendUID!;
    request.httpBody = postParameters.data(using: String.Encoding.utf8)
    let task = URLSession.shared.dataTask(with:request as URLRequest){
        data, response, error in

        if error != nil{
            print("error is \(error)")
            return;
        }

        do{
        let myJSON =  try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
        if let parseJSON = myJSON {
            var uid : String!
            uid = parseJSON["userid"] as! String?

            self.loadedStreet = parseJSON["street"] as! String!     
            self.lblStreet.text = parseJSON["street"] as! String!

            print(uid)
            print(self.loadedStreet)
         }     
    }
    catch {
        print(error)
        }
    }     
    task.resume()
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}
}

2 个答案:

答案 0 :(得分:2)

在主线程上更新您的UI。 URLSession让你在后台线程中。 Swift 3中的相应代码是

DispatchQueue.main.async {
    //your UI code
}

所以你的代码看起来应该更像这样。

class CharDetailViewController: UIViewController {

 //Outlets
@IBOutlet weak var lblUID: UILabel!
@IBOutlet weak var lblStreet: UILabel!

//URL to our web service
let URL_GET_DETAILS = "http://somesite.com/script.php"

var passedUID: String!
var loadedStreet: String!

override func viewDidLoad() {
    super.viewDidLoad()

    let url = URL(string: URL_GET_DETAILS)
    lblUID.text = passedUID

    let request = NSMutableURLRequest(url: url! as URL)
    request.httpMethod = "POST";
    let sendUID = lblUID.text
    let postParameters = "sendUID="+sendUID!;
    request.httpBody = postParameters.data(using: String.Encoding.utf8)
    let task = URLSession.shared.dataTask(with:request as URLRequest){
         data, response, error in

         if error != nil{
             print("error is \(error)")
             return;
         }

         do{
         let myJSON =  try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary

         if let parseJSON = myJSON {
             var uid : String!
             uid = parseJSON["userid"] as! String?

            DispatchQueue.main.async {
              //your UI code
              self.loadedStreet = parseJSON["street"] as! String! 
              self.lblStreet.text = parseJSON["street"] as! String!
            }

           print(uid)
           print(self.loadedStreet)
        }     
    }catch {
     print(error)
    }
 }     
    task.resume()
 }

 override func didReceiveMemoryWarning() {
   super.didReceiveMemoryWarning()
 }
}

答案 1 :(得分:0)

您不应该在主线程以外的任何位置编辑UI。您可以使用此代码获取主线程以实现UI更新。

dispatch_async(dispatch_get_main_queue(), ^{
    // Update UI stuff here
});