Skip to content

Documentation on prefetching. #99

@johndpope

Description

@johndpope
extension TheatreVideoVC: UICollectionViewDataSourcePrefetching {
    // TikTok eager download - fetch 5 movies as soon as possible on app start
    // get the next X videos /  start downloading them...
    func collectionView(_ collectionView: UICollectionView, prefetchItemsAt indexPaths: [IndexPath]) {

if indexPaths.count >= 1 {
                    let indexPath = indexPaths[0]
                    let idx0 = IndexPath(row: indexPath.row, section: 0)
                    let idx1 = IndexPath(row: indexPath.row+1, section: 0)
                    let idx2 = IndexPath(row: indexPath.row+2, section: 0)
                    let idx3 = IndexPath(row: indexPath.row+3, section: 0)
                    let idx4 = IndexPath(row: indexPath.row+4, section: 0)
                    let prefetchIndexPaths = [idx0, idx1, idx2, idx3, idx4]
                    for idx in prefetchIndexPaths {
                        self.prefetchItem(idx: idx)
                    }
                } 
    }

private func prefetchItem(idx: IndexPath) {
        print("INFO:prefetchItem \(idx)")
        if self.feedCollectionView != nil {
            if self.feedCollectionView.isValid(indexPath: idx) {
                if let video = self.theatreDataSource?.itemIdentifier(for: idx) as? Video {
                     Celestial.shared.startDownload(for: video.url)
                }
            }
        }
    }

I used this mistakenly Celestial.shared.prefetchResources(at: [url]) - and performance was crap. Was prepared to throw in bin - but when I switched to simple Celestial.shared.startDownload(for: video.url) - it's all good AND smooth. I think it's the sync calls in prefetch that cause scrolling to come unstuck.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions