1

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")
        }

enter image description here How to remove the warning without bugging my app?

Thanks in advance.

ΩlostA
  • 2,501
  • 5
  • 27
  • 63
  • What about `DispatchQueue.main.sync`? – Sweeper Sep 05 '19 at 16:19
  • @Sweeper it does the same... – ΩlostA Sep 05 '19 at 16:31
  • You will need to tell us what you mean by "application doesn't display correctly". Specifically, what you are expecting to see vs. what you are actually seeing. – Rudedog Sep 05 '19 at 16:52
  • @Rudedog the problem is that if I use a NotificationCenter.default.post in a DispatchQueue.main.sync in a urlSession(_ session: URLSession, downloadTask:, the app doesn't fire the code well. The fact are that my spinner is not well displayed, but it is just un symptom. The main problem is that the code doesn't fire at the good moment. – ΩlostA Sep 06 '19 at 09:34

1 Answers1

0

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

emelagumat
  • 301
  • 2
  • 10