Paul Heinemeyer
Paul Heinemeyer

Reputation: 217

Swift - Force view to load data after appearing

Heyho,

i got a little problem with two of my view controllers. They are both collection view controllers.

My problem is, if i click on the button to open the second view it lags and the transition is not liquid. The reason for this lag is the bunch of data the view loads for the collection view(around 400 photos and strings).

Maybe someone of you got a idea how i could defeat this loading inhibition. If you need more information --> just ask.

Upvotes: 1

Views: 1945

Answers (2)

Praveen Kumar
Praveen Kumar

Reputation: 296

you can use this method to achieve the required result.

override func viewDidLayoutSubviews() {
    // Configure the views 
}

On the other hand, you have self.view.setNeedsLayout() and self.view.layoutIfNeeded(). Find the one which suits you.

Upvotes: 0

Remy Cilia
Remy Cilia

Reputation: 2623

An easy way to fix that is to load you data in a background thread and reload your collection view (in the UI thread) when it's done:

DispatchQueue.global(qos: .background).async {

    // load your data here

    DispatchQueue.main.async {
        // reload your collection view here:
        self.collectionView.reloadData()
    }
}

Upvotes: 2

Related Questions