Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. class CollectionViewController: UICollectionViewController {
  2. func loadForVisibleItems() {
  3. let pathsArray = collectionView.indexPathsForVisibleItems
  4. let allPendingOperations = Set(pendingOperations.downloadsInProgress.keys)
  5. var toBeCancelled = allPendingOperations
  6.  
  7. let visiblePaths = Set(pathsArray)
  8. toBeCancelled.subtract(visiblePaths)
  9.  
  10. var toBeStarted = visiblePaths
  11. toBeStarted.subtract(allPendingOperations)
  12.  
  13. for indexPath in toBeCancelled {
  14. if let pendingDownload = pendingOperations.downloadsInProgress[indexPath] {
  15. pendingDownload.cancel()
  16. }
  17. pendingOperations.downloadsInProgress.removeValue(forKey: indexPath)
  18. }
  19.  
  20. for indexPath in toBeStarted {
  21. let recordToProcess = images[indexPath.row]
  22. startDownload(for: recordToProcess, at: indexPath)
  23. }
  24. }
  25.  
  26. override func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
  27. if !decelerate {
  28. loadForVisibleItems()
  29. resumeAllOperations()
  30. }
  31. }
  32.  
  33. override func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
  34. loadForVisibleItems()
  35. resumeAllOperations()
  36. }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement