Aнгел
Aнгел

Reputation: 1429

Call main thread on UICollectionView didSelectItemAt

I'm using a UICollectionView and a specific process runs when a user clicks on a cell. Since this process may take a few seconds, I want to show a view to the user with some processing message as soon as this process is about to start.

The general idea is as follows:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    DispatchQueue.main.async {
        self.someView.isHidden = false
        print("showing processing message is about to start")
    }

    // some process runs here which may take a few seconds to finish

    DispatchQueue.main.async {
        self.someView.isHidden = true
        print("hidding processing message")
    }

}

For some unknown reason to me, those calls to main thread are performed just after didSelectItemAt has completely finished, thus that processing message disappears almost at the same time it appeared.

Does anybody know what I am missing here?

Thanks :)

Upvotes: 0

Views: 328

Answers (1)

estoril
estoril

Reputation: 115

What you described in your question seems normal based on the code snipped you have entered due to the fact someView is being called on the main thread.

If you would like someView to be hidden upon the process completing, it would be a better idea to hide someView in a completion closure of the processes you intend to run while someView is not hidden.

Upvotes: 1

Related Questions