A 60 fps short-video feed

System design scenarios Senior Swift 5.9+ iOS 17+ Reviewed By Alok Singh

Pick the fix

Swiping between videos stutters and memory keeps growing. Best design?

Options
Hint 1 of 1

How many players are really needed at once?

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A Reuse a pool of about three AVPlayers, preload the next item, and play only the most visible cell

Correct. Fixed memory, and the next video is ready.

Why not the others?
  • B Create a new AVPlayer for every cell. That's expensive and grows memory.
  • C Preload every video in the feed. It wastes bandwidth and memory.
  • D Decode videos to images on the main thread. It blocks scrolling.

30-second answer

Use a paging collection view (or SwiftUI paging scroll view) with a small pool of reusable AVPlayers, perhaps three: current, next and previous. Preload the next items' assets and first segments, start playback only for the most visible cell, and pause and release the rest. Keep the main thread free by loading assets asynchronously and showing a thumbnail until the first frame renders.

Practise saying it · 60 s. No recording, just a timer

Design the video feed.

Say your answer out loud, then check which of these points you covered:

  • Player pool
  • Preload next
  • Play most visible
  • Time to first frame

Deep dive

Creating AVPlayer and AVPlayerItem is expensive; reuse players.

preferredForwardBufferDuration limits buffering for items not yet visible.

Prefetch metadata and thumbnails ahead of video.

Measure hitches and time-to-first-frame.

Common mistakes

  • One AVPlayer per cell for thousands of cells.
  • Playing every visible cell.

Senior take & follow-ups

Seniors pool players, preload one step ahead and measure time-to-first-frame.

Follow-up questions

Think of your answer first, then open the hint.

  1. Why pool players?

    Show hint

    Creation and teardown are expensive and use memory.

  2. What metric matters most?

    Show hint

    Time to first frame while swiping.

Suggest a fix if something here is wrong or out of date.