Dart is dart
Dart is dart

Reputation: 41

isHidden or view.removeFromSuperView are taking a long while

I'm trying to show a UITableView from a button, I tried 2 different ways: One by showing by a UIStackView with was hidden from the beginning, and just show with "isHidden", another way with a view from another UIViewController, which is called with "didMove(toParentViewController: self)" With both ways, the tableview is showing instantly, but to hide it again, that's taking forever. I've tried to put the "isHidden = true" in "DispatchQueue.main.sync" to use the main thread, but still not working as I would like...

Any suggestions?

Here is some code of my application :

@IBAction func ProfilPicture1Pressed(_ sender: UIButton) {

    let popUpTeamDog = storyboard?.instantiateViewController(withIdentifier: "selectTeamDogPopUp") as! SelectTeamDogPopUp
    DispatchQueue.global().async(execute: {
        DispatchQueue.main.sync{
            self.addChildViewController(popUpTeamDog)
            popUpTeamDog.view.frame = self.view.frame
            self.view.addSubview(popUpTeamDog.view)
            popUpTeamDog.didMove(toParentViewController: self)
        }
    })


func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let dog: Dog

    if(searching){
        dog = searchArray[indexPath.row]
    } else {
        dog = dogs[indexPath.row]
    }

    let nameDog = dog.name
    //let imgDog = UIImage(named: dog.image)


    print(nameDog)
    DispatchQueue.global().async(execute: {
        DispatchQueue.main.sync{
            self.view.removeFromSuperview()
        }
    })

So I can add that when I click on the searchBar, then select a dog, the tableView / View disappear instantly, so when the keyboard is activate, that's working well... Stranger things....

Upvotes: 1

Views: 207

Answers (1)

Robert Dresler
Robert Dresler

Reputation: 11150

Your code uses main thread by default. So you don't have to use this DispatchQueue.global() and DispatchQueue.main. It's too slow.

So replace this

DispatchQueue.global().async(execute: {
    DispatchQueue.main.sync{
        self.addChildViewController(popUpTeamDog)
        popUpTeamDog.view.frame = self.view.frame
        self.view.addSubview(popUpTeamDog.view)
        popUpTeamDog.didMove(toParentViewController: self)
    }
})

with

self.addChildViewController(popUpTeamDog)
popUpTeamDog.view.frame = self.view.frame
self.view.addSubview(popUpTeamDog.view)
popUpTeamDog.didMove(toParentViewController: self)

and do the same with self.view.removeFromSuperview(). Replace this

DispatchQueue.global().async(execute: {
    DispatchQueue.main.sync{
        self.view.removeFromSuperview()
    }
})

with

self.view.removeFromSuperview()

Upvotes: 2

Related Questions