0

i have several api call functions in chain as needed showing a PKHUD hud windows the hud works well but i cannot hide the hud in the viewWillDissapear, to make more sence i have profileViewController that is part of UINavigationController nesteed in a tabBarcontroller in view hierarchy like this. enter image description here

in profileVC i have 3 functions to request data from a APIrequest each show a hud like this.

    func loadData(){
        routeClient.instance.getProfile(success: {[weak self] data in
            guard let self = self else {return}
            do{
                let response = try JSONDecoder().decode(profile.self, 
                        if requestManager.instance.user.findHouse! == 0{
                            PKHUD.sharedHUD.contentView = PKHUDProgressView(title: "title", subtitle: "Subtitle")                                
                            PKHUD.sharedHUD.show()
                            self.loadHouse()
                        }else{                               
                            PKHUD.sharedHUD.contentView = PKHUDProgressView (title: "tittle", subtitle: "subtitle")
                            PKHUD.sharedHUD.show()
                            self.loadAnnounce()
                        }                       
                }
            }catch{
                print("error ocurrec in decoder \(error.localizedDescription)")
            }
        }, failure: {error in
            print("error in data load \(error.localizedDescription)")
        })
    }

then in loadHouse and load announce functions with a different message.

func loadHouse(){
    routeClient.instance.getHouseByprofile(success:{[weak self] data in
        guard let self = self else {return}
        PKHUD.sharedHUD.contentView = PKHUDProgressView(title: "Procesando infromacion", subtitle: "")
        PKHUD.sharedHUD.show()
        if requestManager.instance.user.findHouse == 0{
            do{
                let response = try JSONDecoder().decode(houseModel.self, from: data)
                if response.id != nil {
                    self.house = response
                    PKHUD.sharedHUD.contentView = PKHUDSuccessView(title: "title", subtitle: "")
                    PKHUD.sharedHUD.show()
                    PKHUD.sharedHUD.hide(afterDelay: 1.5)
                    self.tableview.reloadData()
                }else{
                    print("the is is nil")
                    PKHUD.sharedHUD.contentView = PKHUDTextView(text: "subtitle")
                    PKHUD.sharedHUD.show()
                    PKHUD.sharedHUD.hide(afterDelay: 1.5)
                    self.tableview.reloadData()
                }
            }catch{
                print("error en la decodificacion \(error.localizedDescription)")
                PKHUD.sharedHUD.contentView = PKHUDErrorView(title: "title,", subtitle: "subtitle")
                PKHUD.sharedHUD.show()
                PKHUD.sharedHUD.hide(afterDelay: 1.5)
                self.tableview.reloadData()
            }
        }else{
            do{
                let response = try JSONDecoder().decode(houseResponse.self, from: data)
                PKHUD.sharedHUD.contentView =  PKHUDErrorView(title: "title", subtitle: "subtitle")
                PKHUD.sharedHUD.show()
                PKHUD.sharedHUD.hide(afterDelay: 1.5)
                self.tableview.reloadData()
            }catch{
                PKHUD.sharedHUD.contentView = PKHUDErrorView(title: "title,", subtitle: "subtitle")
                PKHUD.sharedHUD.show()
                PKHUD.sharedHUD.hide(afterDelay: 1.5)
            }
            self.tableview.reloadData()
        }
    }, failure: {[weak self] error in
        guard let self = self else {return}
        print(error.localizedDescription)
        self.tableview.reloadData()
    })
}
func loadAnnounce(){
    routeClient.instance.getAnnounceByProfile(success: {[weak self] data in
        guard let self = self else {return}
        do{
            let json = try JSONDecoder().decode(getAnnounceResponse.self, from: data)
            PKHUD.sharedHUD.hide()
            if json.success!{
                requestManager.instance.announce = (json.data?.count)! > 0 ? json.data![0] : nil
                self.tableview.reloadData()
            }
        }catch{
            PKHUD.sharedHUD.hide()
            print(error.localizedDescription)
        }
    }, failure: {error in
        PKHUD.sharedHUD.hide()
        print(error.localizedDescription)
    })
}

all that is working fine as it should, the thing is i i change the tab in tabbarcontroller and the hud is not yet shown in the profileVC then is loaded as profileVC were the the presented viewController. what i tried to hide the HUD was the following.

override func viewWillDisappear(_ animated: Bool) {
    //PKHUD.sharedHUD.hide()
    print("va a desaparecer")
    PKHUD.sharedHUD.hide()
    NotificationCenter.default.removeObserver(self, name: .didchangeTab, object: nil)
    self.navigationController?.navigationBar.setBackgroundImage(nil, for: .default)
    //self.navigationController?.navigationBar.barTintColor = .clear//UIColor.init(red: 106/255, green: 172/255, blue: 82/255, alpha: 1)
}
override func viewDidDisappear(_ animated: Bool) {
    PKHUD.sharedHUD.hide()
}

but none works, also added a notification to notification center to observer when tabBarController change tabbar item but even the hud does not hide, this only happens when the profileVC is loading for the first time and HUD is now shown yet, so if user tap on next tabbar item then the other viewcontrollers load and then the HUD is Shown. I also check for memory leaks but not glue why is no dealocation from memory the viewcontroller even when viewWillDissappear is executin.

Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
Yoel Jimenez del valle
  • 1,270
  • 1
  • 9
  • 20
  • Not an answer to your specific question, but wouldn't it make more sense to show the hud before you start the async request? That is when the loading begins. This would also prevent the user from navigating to another tab until the loading is done (if you HUD prevents input). – Sune Jan 29 '19 at 08:16
  • thas's a good point, i just dont want tomake the users wait for all the request end to let them explore the app. but i guess that may tha'ts could avoid all probelms – Yoel Jimenez del valle Jan 29 '19 at 12:17
  • If you want them to be able to explore the app while loading data, then you shouldn't use an activity indicator that blocks the entire app (pkhud). You could manually add an activity indicator to the specific view controller that needs to be blocked while loading.https://developer.apple.com/documentation/uikit/uiactivityindicatorview – Sune Jan 30 '19 at 06:43

0 Answers0