Creating and destroying AVFoundation classes on background queues? Creating and destroying AVFoundation classes on background queues? multithreading multithreading

Creating and destroying AVFoundation classes on background queues?


The following will definitely hurt scrolling performance

  • Creating AVPlayer, 10 fps drop
  • Adding player to AVPlayerLayer, 15 fps drop
  • Start play, 15/25 fps drop
  • Pause play, 15 fps drop
  • Deallocate AVPlayer, 20+ fps drop

I tried with allocate AVPlayer from a background thread, however, AVFoundation will switch back to main and perform the allocation/initialization from main. Same with play/pause.

I have some success with deallocating AVPlayer/AVPlayerItem on a background thread. Basically you need to clear out last reference to AVPlayer from your background thread.


Generally speaking, if it isn't documented to be thread safe in the classes documentation, you shouldn't use an object of that class across multiple threads.

More to the point, while there's a benefit to doing less on the main thread, allocating AVPlayer, AVPlayerItem and AVURLAsset's won't ever practically be a bottleneck that you need to worry about. Instead, you should run Instruments and find real performance problems, and focus on those instead.