基本上我尝试创建一个设置多个通知的功能,当它执行此操作时,我希望它在警报视图中显示加载/进度条,并在设置通知时更新它。
问题是,当我将进度视图作为子视图添加到警报视图时,UI不会更新,直到处理完成。
通常我会使用' DispatchQueue.main.async'解决此类问题。但这似乎并没有什么不同。
以下是代码。在“现场观看”中功能,“SetUpNotifications”中的处理'作为' viewPresented'传递的函数在UI实际显示' progressView'之前,闭包功能完成。已添加到' loadingView'。
我很确定这是一个线程问题,因为在使用' DispatchQueue.main.async'时它似乎无法到达主UI线程,可能与它有关有一个@escaping参数?或者仅仅是封闭内的闭包这一事实?
var centre: UNUserNotificationCenter?
func AttemptToSetUpNotifications() {
centre = UNUserNotificationCenter.current()
centre!.requestAuthorization(options: [.alert, .badge], completionHandler: SetUpNotificationsChecks)
}
func SetUpNotificationsChecks(granted: Bool, error: Error?) {
if error != nil {
return
}
if !granted {
return
}
PresentLoadingView(viewPresented: SetUpNotifications)
}
var loadingView: UIAlertController?
func PresentLoadingView(viewPresented: @escaping ((UIProgressView)-> Void)) {
loadingView = UIAlertController(title: "Setup Notifications", message: "Setting up notifications, please wait...", preferredStyle: .alert)
self.present(loadingView!, animated: true, completion: {
// Add your progressbar after alert is shown
let margin:CGFloat = 8.0
let rect = CGRect(x:margin, y:72.0, width:self.loadingView!.view.frame.width - margin * 2.0 , height:2.0)
let progressView = UIProgressView(frame: rect)
progressView.progress = 0.0
progressView.tintColor = Common.CommonTintColor
self.loadingView!.view.addSubview(progressView)
// DispatchQueue.main.async {
// self.loadingView!.view.addSubview(progressView)
// }
viewPresented(progressView)
})
}
任何帮助都将不胜感激,如果问题是我缺乏并发知识,请对不起,如果您需要更多信息,请询问。
感谢。