My UICollectionView is fetching images in cellForItemAt
. After I upgrade to swift 3, some images are not showing when I scroll very fast. Here is my code in cellForItemAt
:
if (imageNotInCache) {
DispatchQueue.global(qos: DispatchQoS.QoSClass.default).async {
if let thumb = imageFromPathAndCacheIt(path) {
DispatchQueue.main.async {
let updateCell = collectionView.cellForItem(at: indexPath) as! MyCustomCell? {
updateCell.imageView.image = thumb
}
}
}
The problem is, sometimes I get updateCell
as nil even it is on the screen (probably because scroll to fast).
I know we can add reloadData
after adding the image, just like: Swift 3: Caching images in a collectionView, but I don't want to call reload so many times.
Thank you for interested in this question. Any ideas would be very appreciated!