I am trying to create an application that has two collection views in one. I want to first populate the first collection view, and then the second. First, I set the delegate of photoCollectionView
to self
. Then, I populated the collectionview by calling photoCollectionView.reloadData()
. However, I want photoCollectionView.reloadData()
to first complete before any further code is executed because when I included the code underneath it, the collection view I was initially trying to populate doesn't populate. However, when I remove the code, it does populate. I tried to use DispatchQueue, but I had no luck.
photoCollectionView.dataSource = self
photoCollectionView.reloadData() // I want this code to complete before executing anything else under it.
media.removeAll()
let assetVideo = PHAsset.fetchAssets(with: PHAssetMediaType.video , options: nil)
assetVideo.enumerateObjects({ (object, count, stop) in
self.media.append(object)
})
self.media.reverse()
videoControllerView.dataSource = self
videoControllerView.reloadData()
Code after adding DispatchQueue. When executing this code, the collection view does not populate. However, if we comment out all the code in the DispatchQueue, it populates.
photoCollectionView.dataSource = self
photoCollectionView.reloadData()
DispatchQueue.main.async {
self.media.removeAll()
let assetVideo = PHAsset.fetchAssets(with: PHAssetMediaType.video , options: nil)
assetVideo.enumerateObjects({ (object, count, stop) in
self.media.append(object)
})
self.media.reverse()
self.videoControllerView.dataSource = self
self.videoControllerView.reloadData()
}
Any help is appreciated. Thanks!