title: Video preload in uicollectionview in swift
body: <p>I am developing Instagram-like an app that has insta reels-like UI where videos are played in the list but what happens now is when cell comes on scree video buffers or loads and it takes 1-2 sec to load because of that uicollectionview scroll lags.</p>
  <pre><code>func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
      let currentItem = appData[indexPath.item]
      
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: &quot;swipablePostCollectionViewCell&quot;, for: indexPath) as! swipablePostCollectionViewCell
      
      preFetchNextItems(currentIndexPath: indexPath)
      
      if currentItem[&quot;video_url&quot;] != &quot;&quot; &amp;&amp; currentItem[&quot;video_url&quot;] != nil {
          cell.postVideoView.isHidden = false
          cell.postImageView.isHidden = true
          cell.postVideoView.tag = indexPath.item
          DispatchQueue.main.async {
          if let imageURL: URL = URL(string: currentItem[&quot;video_url&quot;]!) {
              if indexPath.item == 0 {
                  let item = VersaPlayerItem(url: imageURL)
                  cell.postVideoView.set(item: item)
                  cell.postVideoView.play()
              } else {
                  if let newItem = self.prefetchedData[indexPath.item - 1] as? VersaPlayerItem {
                      cell.postVideoView.set(item: newItem)
                  }
              }
              cell.postVideoView.controls?.behaviour.shouldHideControls = false
              cell.postVideoView.isPipModeEnabled = false
              cell.postVideoView.autoplay = false
              cell.postVideoView.renderingView.playerLayer.videoGravity = AVLayerVideoGravity.resizeAspect
              cell.postVideoView.layer.backgroundColor = UIColor.black.cgColor
              cell.postVideoView.use(controls: cell.controls)
              cell.postVideoView.playbackDelegate?.startBuffering(player: cell.postVideoView.player)
              cell.tag = indexPath.item
          }
          }
          return cell
      } else {
          cell.postVideoView.isHidden = true
          cell.postImageView.isHidden = false
          if let imageURL: URL = URL(string: currentItem[&quot;image_url&quot;] ?? &quot;&quot;) {
              cell.postImageView.sd_setImage(with: imageURL, placeholderImage: UIImage(named: &quot;error&quot;), options: .delayPlaceholder, context: nil)
              cell.postImageView.contentMode = .scaleAspectFill
              cell.postImageView.clipsToBounds = true
          }
          return cell
      }
  }
  </code></pre>
  <p>And for prefetch</p>
  <pre><code>func preFetchNextItems(currentIndexPath: IndexPath?) {
      if currentIndexPath?.item == 0 {
          for i in 1...4 {
              let indexP = IndexPath(item: i, section: 0)
              arrFetchedIndexPaths.append(indexP)
              lastFetchedIndexPath = indexP
              GetDataForPrefetchIndexPath()
          }
      } else {
          let newIndePath = IndexPath(item: lastFetchedIndexPath!.item + 1, section: lastFetchedIndexPath!.section)
          arrFetchedIndexPaths.append(newIndePath)
          lastFetchedIndexPath = newIndePath
          GetDataForPrefetchIndexPath()
      }
       print(&quot;Prefetch indexpath - &quot;, arrFetchedIndexPaths)
  }
  
  func GetDataForPrefetchIndexPath() {
      if appData.count &gt; lastFetchedIndexPath!.item {
          let currentItem = appData[lastFetchedIndexPath!.item]
          if currentItem[&quot;video_url&quot;] != &quot;&quot; &amp;&amp; currentItem[&quot;video_url&quot;] != nil {
              if let imageURL: URL = URL(string: currentItem[&quot;video_url&quot;]!) {
                      let newItem = VersaPlayerItem(url: imageURL)
                  prefetchedData.append(newItem)
              }
          } else {
              if let imageURL: URL = URL(string: currentItem[&quot;image_url&quot;] ?? &quot;&quot;) {
  //                let newImage = sd_setImage(with: imageURL, placeholderImage: UIImage(named: &quot;error&quot;), options: .delayPlaceholder, context: nil)
                  prefetchedData.append(imageURL)
              }
          }
              for item in prefetchedData {
                  print(item)
              }
      }
  }
  </code></pre>
  <p><strong>What i need is on first cell next 4 cell video should preloaded and after user scrolls next cell video should load
  so that when user scrolls to next cell video will be preloaded and played without buffer.</strong></p>