繁体   English   中英

iOS:只能从主线程使用UIWindow.rootViewController

[英]iOS: UIWindow.rootViewController must be used from main thread only

我有一个问题

if let wd = UIApplication.shared.delegate?.window {
            var vc = wd!.rootViewController

如果我将这段代码放在Dispatch中,警告消息会消失,但是应用程序无法正确显示。 如果我删除该调度,则有警告消息。

UIWindow.rootViewController必须仅在主线程中使用

UIApplication.delegate必须仅在主线程中使用

该类专门用于使用progressBar下载。

      public func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
            print("Download finished: \(location)")
...
 do { 
            let result =  try FileManager.default.replaceItemAt(URL(fileURLWithPath: Constants.Path.temp.stringByAppendingPathComponent(path: "temp.zip")), withItemAt: URL(fileURLWithPath: location.path))

            let source = Constants.Path.tempZipFile
            let destination = Constants.Path.temp.stringByAppendingPathComponent(path: "dezipped")
            var myDict = [String:Any]()
            myDict["source"] = source
            myDict["destination"] = destination


            DispatchQueue.main.async { //IF I REMOVE THIS => PB OR THREAD IN MAIN
            if let wd = UIApplication.shared.delegate?.window {
                var vc = wd!.rootViewController
                if(vc is UINavigationController){
                    vc = (vc as! UINavigationController).visibleViewController

                }

                if(vc is WebViewController){
                    NotificationCenter.default.post(name: .DeflatSynchroFilesWebView, object: myDict, userInfo: nil)
                }
                else
                {
                    NotificationCenter.default.post(name: .DeflatSynchroFiles, object: myDict, userInfo: nil)
                }
            }
            }
        } catch let writeError as NSError {
            print("error writing file temp.zip to temp folder")
        }

在此处输入图片说明 如何在不打扰我的应用的情况下删除警告?

提前致谢。

我不确定这是否有帮助,但是我总是使用以下rootViewController来获取rootViewController

if let window = UIApplication.shared.keyWindow?.rootViewController {

        }

没有代表

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM