hide the pkhud HUD does not work on viewWillDissapear

415 Views Asked by At

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.

0

There are 0 best solutions below