简体   繁体   中英

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

I have a problem by using

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

If I put this piece of code in a Dispatch, the warning message disappear, but the application doesn't display correctly. If I remove the dispatch, I have warning message.

UIWindow.rootViewController must be used from main thread only

AND

UIApplication.delegate must be used from main thread only

That class is specially for downloading with a 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")
        }

在此处输入图片说明 How to remove the warning without bugging my app?

Thanks in advance.

I am not sure if this can help, but to get the rootViewController I always use this:

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

        }

without the delegate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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