Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation on prefetching. #99

Open
johndpope opened this issue Jul 29, 2021 · 0 comments
Open

Documentation on prefetching. #99

johndpope opened this issue Jul 29, 2021 · 0 comments

Comments

@johndpope
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant