我有一個從互聯網加載數據的應用程序。從互聯網加載數據時,應用程序正在凍結。加載時是否可以顯示警報視圖?你可以看到上面的didSelectRowAtIndexPath:func。應用程序加載數據並凍結時是否可以顯示警報?
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath)! as UITableViewCell
let text = cell.textLabel?.text!
let indexNumber = towersNameArray.indexOf(text!)
PlayData.TowerAddress = towersAddressArray[indexNumber!] //Dataloading
PlayData.TowerName = towersNameArray[indexNumber!] //Dataloading
if #available(iOS 8.0, *) {
let alertController = UIAlertController(title: "Loading...", message: "Please Wait", preferredStyle: .Alert)
self.presentViewController(alertController, animated: true, completion: nil)
let delay = 2.5 * Double(NSEC_PER_SEC)
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
dispatch_after(time, dispatch_get_main_queue(), {
alertController.dismissViewControllerAnimated(true, completion: nil)
})
tabBarController?.selectedIndex = 1
} else {
// Fallback on earlier versions
}
}
在主/ UI線程中加載數據是一個壞主意。對數據加載使用異步調用 –