Ios 带使用通知中心的加载器

Ios 带使用通知中心的加载器,ios,swift,Ios,Swift,我有密码: MainView.swift: override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(true) // Register to receive notification NotificationCenter.default.addObserver(self, selector: #selector(MainViewControler.StartUpda

我有密码:

MainView.swift:

override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(true)
        // Register to receive notification
        NotificationCenter.default.addObserver(self, selector: #selector(MainViewControler.StartUpdatingSplash), name: NSNotification.Name("StartUpdatingSplashNotificationName"), object: nil)

        //new code
        NotificationCenter.default.addObserver(self, selector: #selector(MainViewControler.FinishUpdatingSplash), name: NSNotification.Name("FinishUpdatingSplashNotificationName"), object: nil)

    }

    @objc func StartUpdatingSplash() {
        DispatchQueue.global().async {
            EZLoadingActivity.show("LoadingMessage4".localized(), disableUI: true)
        }
        print("##### NOTIFICATION STEP: 1")
    }

    @objc func FinishUpdatingSplash() {
        DispatchQueue.global().async {
            EZLoadingActivity.hide()
        }
        // Stop listening notification
        NotificationCenter.default.removeObserver(self, name: Notification.Name("StartUpdatingSplashNotificationName"), object: nil)
        print("##### NOTIFICATION FINISH STEP: 2")
    }
App.swift:

var filesToDownload = [FilesToDownload]()
var filesToDownloadPDF = [FilesToDownload]()

struct FilesToDownload {
    var fileInternetUrl: String?
    var fileName: String?
    var savedURL: String?
    var productImageUrl: URL?
    var fileSize: Int
}    

func startDownloadFiles(filesArray: [FilesToDownload], filesType: Int){
        // Post notification
        NotificationCenter.default.post(name: Notification.Name("StartUpdatingSplashNotificationName"), object: nil)
        for files in filesArray{
            if filesType == 1 {
                print ("PLIKI DO SCIAGNIECIA: \(files)")
                checkRemoteImage(fileInternetUrl: files.fileInternetUrl!, fileName: files.fileName!, savedURL: files.savedURL!, productImageUrl: files.productImageUrl!, fileSize: files.fileSize)
            } else {
                print ("PLIKI DO SCIAGNIECIA PDF: \(files)")
                checkRemotePdf(fileInternetUrl: files.fileInternetUrl!, fileName: files.fileName!, savedURL: files.savedURL!, productPdfUrl: files.productImageUrl!, fileSize: files.fileSize)
            }
        }
    }

func checkRemoteImage(fileInternetUrl: String, fileName: String, savedURL: String, productImageUrl: URL, fileSize: Int){
        remoteResource(at: productImageUrl, fileSize: fileSize) { (isImage) in
            if isImage == true {
                self.saveDownloadFiles(fileInternetUrl: productImageUrl, fileName: fileName, savedURL: savedURL)
            }
        }
    }

    func checkRemotePdf(fileInternetUrl: String, fileName: String, savedURL: String, productPdfUrl: URL, fileSize: Int){
        self.saveDownloadFiles(fileInternetUrl: productPdfUrl, fileName: fileName, savedURL: savedURL)
    }


    func saveDownloadFiles(fileInternetUrl: URL, fileName: String, savedURL: String){
        let cms = ServerConnect()
        cms.downloadedFileFromInternet(fileInternetUrl: fileInternetUrl, directory: savedURL, fileName: fileName ,  completion: { (data) in
            switch data {
            case .succes:
                print("DOWNLOAD: \(savedURL)/\(fileName)")
            case .error(let error):
                //self.errorLoginMessage(txt: "MainView - Error 110: Problem with download images. \(error)", title: "Blad".localized())
                print("")
                break
            }
        })
    }


func downloadedFileFromInternet(fileInternetUrl: URL, directory: String, fileName: String , completion: @escaping completionHandler) {
        if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
            let fileURL = dir.appendingPathComponent(directory)

            if !FileManager.default.fileExists(atPath: fileURL.path) {
                do {
                    try FileManager.default.createDirectory(atPath: fileURL.path, withIntermediateDirectories: true, attributes: nil)
                } catch {
                    NSLog("Couldn't create document directory")
                }
            }
            do {
                let data = try? Data(contentsOf: fileInternetUrl)
                try data?.write(to: fileURL.appendingPathComponent(fileName), options: .atomic)
            } catch let error {
                print("Error : \(error.localizedDescription)")
            }

        } 
    }
在函数startDownloadFiles中,我开始下载我的文件。 我想在下载文件时显示加载程序
(EZLoadingActivity)

目前,启动splash
(loader EZLoadingActivity)
应用程序后,splash始终可见,下载所有文件后不会隐藏


从文件下载完所有文件后,如何隐藏这些
EZLoadingActivity

let cms = ServerConnect()
    cms.downloadedFileFromInternet(fileInternetUrl: fileInternetUrl, directory: savedURL, fileName: fileName ,  completion: { (data) in

       NotificationCenter.default.post(name: Notification.Name("FinishUpdatingSplashNotificationName"), object: nil)

        switch data {
        case .succes:
            print("DOWNLOAD: \(savedURL)/\(fileName)")
        case .error(let error):
            //self.errorLoginMessage(txt: "MainView - Error 110: Problem with download images. \(error)", title: "Blad".localized())
            print("")
            break
        }
    })
我注意到很少有其他地方可以打网络电话。所以,如果您在使用这些方法发出请求之前显示loader,那么您也应该在那里触发此通知。e、 g,

URLSession.shared.dataTask(with: fileUrl) { (data, response
        , error) in
    NotificationCenter.default.post(name: Notification.Name("FinishUpdatingSplashNotificationName"), object: nil)
}
然后,在侦听器方法中,您应该有逻辑来知道隐藏加载程序的时机是否正确


建议使用通知不是处理您试图处理的内容的好方法。这将为你管理比赛条件增加很多努力。我建议您检查
PromiseKit
或至少有效地使用
完成处理程序。

如果您发布“FinishUpdatingSplashNotificationName”此通知,我不会将FinishUpdatingSplashNotificationName发送到任何地方。我不知道在哪里补充:(首先,您需要将所有下载任务和观察者排队,并发出通知。如果队列为空,则可以使用信号灯或DispatchGroups将任务排队。您可以这样做:“
下载文件fromInternet
方法不涉及internet。您应该发出
FinishUpdatingSplashNotificationName
当您得到响应时(成功/失败)从请求中。如果您可以显示发出请求的实际代码,那么我们可以帮助您告诉您可以发出此通知的确切行。感谢您的回答。我将立即检查您的代码。为什么不使用NotificationCenter?是否最好这样做:?此代码
如果filesToDownload.count>0{EZLoadingActivity.hide}
将在下载第一个文件后隐藏您的加载程序。不是全部。除了通知触发的方法外,您不应将加载程序隐藏在任何位置。使其成为标准。在该方法中,检查是否下载了所有文件,然后隐藏。