Отображение оповещения на главном экране функции из другого файла - PullRequest
0 голосов
/ 08 июня 2018

У меня есть этот код:

MainViewControler:

func errorLoginMessage(txt: String, title: String){
        let alertController = UIAlertController(title: title, message: txt, preferredStyle: .alert)
        alertController.addAction(UIAlertAction(title: "Ok".localized(), style: .cancel, handler: { (action: UIAlertAction!) in
            exit(0)
        }))
        self.present(alertController, animated: true, completion: nil)
    }

и файлы в функции AppSystem.swift:

func startUpdate(){ 
        let dispatchGroup = DispatchGroup()
        dispatchGroup.enter()

        let cms = ServerConnect()
        cms.getJsonProducts(completion: { (data) in
            switch data {
            case .succes(let data):
                self.saveJsonFileToTheDiskProducts(downloadData: data)
            case .error(let error):
                self.errorLoginMessage(txt: "MainView - Error 101: Configuration files can not be created. \(error)", title: "Blad".localized())
                print("")
                break
            }
            dispatchGroup.leave()
        })
    }

У меня ошибка с этим:

self.errorLoginMessage(txt: "MainView - Error 101: Configuration files can not be created. \(error)", title: "Blad".localized())

: значение типа 'AppSystem' не имеет члена 'errorLoginMessage'

Кто-нибудь знает, как это исправить?

1 Ответ

0 голосов
/ 08 июня 2018

Поскольку оповещение не является частью вашего AppSystem класса, вам необходимо передать сообщение на MainViewController, чтобы показать сообщение в случае сбоя.

Вы можете использовать Delegates, Notifiations или Completion blocks, это пример completion block:

func startUpdate(completion:@escaping(Bool)->()){
     // No need of DispatchGroup as pointed by Anbu.Karthik, as with completion block you are making this function work asynchronously only.
    //let dispatchGroup = DispatchGroup()
    //dispatchGroup.enter()

    let cms = ServerConnect()
    cms.getJsonProducts(completion: { (data) in
        switch data {
        case .succes(let data):
            self.saveJsonFileToTheDiskProducts(downloadData: data)
            completion(true)
        case .error(let error):
            completion(false)
            //self.errorLoginMessage(txt: "MainView - Error 101: Configuration files can not be created. \(error)", title: "Blad".localized())
            print("")
            break
        }
        //dispatchGroup.leave()
    })
}

Назовите это так в MainViewController:

startUpdate { (success) in
    if !success {
        //Show alert
        self.errorLoginMessage(txt: "MainView - Error 101: Configuration files can not be created. \(error)", title: "Blad".localized())
    }
}
...