Ios 应用程序加载数据并冻结时是否可能显示警报?
我有一个从互联网加载数据的应用程序。从internet加载数据时,应用程序处于冻结状态。加载时是否可以显示警报视图?您可以看到上面的didSelectRowAtIndexPath:funcIos 应用程序加载数据并冻结时是否可能显示警报?,ios,swift,uitableview,uialertview,Ios,Swift,Uitableview,Uialertview,我有一个从互联网加载数据的应用程序。从internet加载数据时,应用程序处于冻结状态。加载时是否可以显示警报视图?您可以看到上面的didSelectRowAtIndexPath:func func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { let cell = tableView.cellForRowAtIndexPath(indexPath)! as
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
}
}
您应该在后台线程中加载数据,如下所示:
//display an alert here if you need
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
//load stuff from internet here, don't display an alert
dispatch_async(dispatch_get_main_queue(), {
//display an alert here if you need to
})
})
//display an alert here if you need.
或者:
您可以将计时器设置为在开始加载内容前约5秒后启动,并且在内容完成后可以使计时器无效。这样,如果内容在5秒内未加载,计时器将启动,如果加载速度更快,计时器将不会启动
func load() {
self.timer = NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: "timerDone", userInfo: nil, repeats: false)
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
//load stuff from internet here
dispatch_async(dispatch_get_main_queue(), {
self.timer.invalidate()
})
})
}
func timerDone() {
//display alert here
}
您应该在后台线程中加载数据,如下所示:
//display an alert here if you need
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
//load stuff from internet here, don't display an alert
dispatch_async(dispatch_get_main_queue(), {
//display an alert here if you need to
})
})
//display an alert here if you need.
或者:
您可以将计时器设置为在开始加载内容前约5秒后启动,并且在内容完成后可以使计时器无效。这样,如果内容在5秒内未加载,计时器将启动,如果加载速度更快,计时器将不会启动
func load() {
self.timer = NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: "timerDone", userInfo: nil, repeats: false)
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
//load stuff from internet here
dispatch_async(dispatch_get_main_queue(), {
self.timer.invalidate()
})
})
}
func timerDone() {
//display alert here
}
为了用另一种方法解决这个问题,我将加载数据从internet部分移动到Disclesh方法之前的dispatch部分。通过使用这种方法,当数据加载时,警报将离开。(如果输入的时间小于加载计时器的时间)为了用另一种方法解决此问题,我将加载数据从internet部件移动到Dismise方法之前的dispatch部件。通过使用这种方式,当数据加载时警报会离开。(如果输入的时间少于加载计时器的时间)在主/UI线程上加载数据是个坏主意。对数据加载使用异步调用在主/UI线程上加载数据不是一个好主意。对数据加载使用异步调用