Prefetching
Aside from the previous section on Global Properties, Celestial offers two functions prefetching media and cancelling those prefetch requests respectively. This is works in tandem with the UICollectionView and UITableView prefetching protocols
Downloads for Prefetched items
The Apple prefetching protocol function notifies the receiver of cells that are about to be displayed which gives Celestial the opportunity to begin or cancel asynchronous loading of media.
Begins download on multiple requested resource
public func prefetchResources(at urlStrings: [String])Pauses (and cancels if desired) downloads on multiple requested resources.
public func pausePrefetchingForResources(at urlStrings: [String], cancelCompletely: Bool)Example
// Using generic struct to display each cell
struct CellModel {
let urlString: String
}
extension ViewController: UICollectionViewDataSourcePrefetching {
func collectionView(_ collectionView: UICollectionView, prefetchItemsAt indexPaths: [IndexPath]) {
let prefetchedModels = getCellModels(at: indexPaths)
let urlStrings = prefetchedModels.map { $0.urlString }
Celestial.shared.prefetchResources(at: urlStrings)
}
func collectionView(_ collectionView: UICollectionView, cancelPrefetchingForItemsAt indexPaths: [IndexPath]) {
let prefetchedModels = getCellModels(at: indexPaths)
let urlStrings = prefetchedModels.map { $0.urlString }
Celestial.shared.pausePrefetchingForResources(at: urlStrings, cancelCompletely: false)
}
func getCellModels(at indexPaths: [IndexPath]) -> [CellModel] {
// Return the cell items at these indexPaths ...
}
}