Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class CollectionViewController: UICollectionViewController {
- func loadForVisibleItems() {
- let pathsArray = collectionView.indexPathsForVisibleItems
- let allPendingOperations = Set(pendingOperations.downloadsInProgress.keys)
- var toBeCancelled = allPendingOperations
- let visiblePaths = Set(pathsArray)
- toBeCancelled.subtract(visiblePaths)
- var toBeStarted = visiblePaths
- toBeStarted.subtract(allPendingOperations)
- for indexPath in toBeCancelled {
- if let pendingDownload = pendingOperations.downloadsInProgress[indexPath] {
- pendingDownload.cancel()
- }
- pendingOperations.downloadsInProgress.removeValue(forKey: indexPath)
- }
- for indexPath in toBeStarted {
- let recordToProcess = images[indexPath.row]
- startDownload(for: recordToProcess, at: indexPath)
- }
- }
- override func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
- if !decelerate {
- loadForVisibleItems()
- resumeAllOperations()
- }
- }
- override func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
- loadForVisibleItems()
- resumeAllOperations()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement