NotificationCenter - 显示和隐藏启动画面

时间:2018-06-19 08:57:01

标签: ios swift

我有这段代码:

MainViewControler:

override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(true)
        // register  notification
        NotificationCenter.default.addObserver(self, selector: #selector(MainViewControler.StartUpdatingSplash), name: NSNotification.Name("updating.salestool"), 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()
    }
    NotificationCenter.default.removeObserver(self, name: Notification.Name("updating.salestool"), object: nil)
    print("##### NOTIFICATION STEP: 2")
}

和Config.swift:

NotificationCenter.default.post(name: NSNotification.Name("updating.salestool"), object: nil)

let dispatchImagesGroup = DispatchGroup()
                dispatchImagesGroup.enter()
                DispatchQueue.global().async {
                    self.downloadImages(toDownloads: jsonData, savedURL: FileFolders.GET_PHOTO.rawValue, parametr: FileFolders.GET_PHOTO.rawValue)
                    dispatchImagesGroup.leave()
                }

                dispatchImagesGroup.enter()
                DispatchQueue.global().async {
                    self.downloadImages(toDownloads: jsonData, savedURL: FileFolders.GET_INSPIRATION_PHOTO.rawValue , parametr: FileFolders.GET_INSPIRATION_PHOTO.rawValue)
                    dispatchImagesGroup.leave()
                }

                dispatchImagesGroup.enter()
                DispatchQueue.global().async {
                    self.downloadImages(toDownloads: jsonData, savedURL: FileFolders.GET_PACKSHOT.rawValue , parametr: FileFolders.GET_PACKSHOT.rawValue)
                    dispatchImagesGroup.leave()
                }


                dispatchImagesGroup.enter()
                DispatchQueue.global().async {
                    self.downloadImages(toDownloads: jsonData, savedURL: (AppGlobalManager.sharedManager.loggedUser?.selectedLanguage)! + "/" + FileFolders.GET_TIPS_SLIDES.rawValue, parametr: FileFolders.GET_TIPS_SLIDES.rawValue)
                    dispatchImagesGroup.leave()
                }

                dispatchImagesGroup.enter()
                DispatchQueue.global().async {
                    self.downloadImages(toDownloads: jsonData, savedURL: (AppGlobalManager.sharedManager.loggedUser?.selectedLanguage)! + "/" + FileFolders.GET_LEAFLETS_SLIDES.rawValue, parametr: FileFolders.GET_LEAFLETS_SLIDES.rawValue)
                    dispatchImagesGroup.leave()
                }

                dispatchImagesGroup.enter()
                DispatchQueue.global().async {
                    self.downloadImages(toDownloads: jsonData, savedURL: (AppGlobalManager.sharedManager.loggedUser?.selectedLanguage)! + "/" + FileFolders.GET_CONCEPTS_SLIDES.rawValue, parametr: FileFolders.GET_CONCEPTS_SLIDES.rawValue)
                    dispatchImagesGroup.leave()
                }

                dispatchImagesGroup.enter()
                DispatchQueue.global().async {
                    self.downloadLeafletsPDF(toDownloads: jsonData, savedURL: (AppGlobalManager.sharedManager.loggedUser?.selectedLanguage)! + "/" + FileFolders.GET_LEAFLETS_PDF.rawValue)
                    dispatchImagesGroup.leave()
                }

                dispatchImagesGroup.notify(queue: .global()) {
                NotificationCenter.default.addObserver(self, selector: #selector(MainViewControler.FinishUpdatingSplash), name: NSNotification.Name("updating.salestool.aviko.qbmobile.com"), object: nil)
            }

StartUpdatingSplash - 运行启动。

功能downloadImages - 运行从互联网下载照片的线程。 启动后的应用程序显示splash(EZLoadingActivity),然后下载照片。

我希望在完成用于下载照片的所有这些线程之后隐藏EZLoadingActivity(EZLoadingActivity.hide),例如通过运行FinishUpdatingSplash()函数。我怎么能这样做?

我的通知中心正确显示启动 - 我有一个问题就是隐藏它

2 个答案:

答案 0 :(得分:0)

在主异步队列中执行隐藏任务。

答案 1 :(得分:0)

函数downloadImages是否在MainViewControler类中?

如果是,您可以直接调用FinishUpdatingSplash:

    dispatchImagesGroup.notify(queue: .global()) {
            self.FinishUpdatingSplash()
        }

如果错误,请在完成所有下载任务后发布通知。

    dispatchImagesGroup.notify(queue: .global()) {
              NotificationCenter.default.post(name: NSNotification.Name("FinishUpdatingSplashNotificationName"), object: nil
        }

您需要像在StartUpdatingSplash中那样在MainViewControler中添加此通知的观察者:

    override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)
    // register  notification
    NotificationCenter.default.addObserver(self, selector: #selector(MainViewControler.StartUpdatingSplash), name: NSNotification.Name("updating.salestool.aviko.qbmobile.com"), object: nil)

    //new code 

    NotificationCenter.default.addObserver(self, selector: #selector(MainViewControler.StartUpdatingSplash), name: NSNotification.Name("FinishUpdatingSplashNotificationName"), object: nil)
}