From 08410c77c49182093022d2c09959f9908bd2bd45 Mon Sep 17 00:00:00 2001 From: Joel Bell Date: Fri, 17 Mar 2017 15:33:40 -0700 Subject: [PATCH] Moved Creation of AVURLAsset to background thread. AVURLAsset has some undocumented behavior where passing specific url's to it with the AVAsset(url:) initializer will cause main thread stalls. I simply moved the creation of this object onto a background thread to avoid any of these potential stalls happening on the main thread. --- Source/JukeboxItem.swift | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/Source/JukeboxItem.swift b/Source/JukeboxItem.swift index 0157bfa..35aa504 100644 --- a/Source/JukeboxItem.swift +++ b/Source/JukeboxItem.swift @@ -168,13 +168,14 @@ open class JukeboxItem: NSObject { } fileprivate func loadAsync(_ completion: @escaping (_ asset: AVURLAsset) -> ()) { - let asset = AVURLAsset(url: URL, options: nil) - - asset.loadValuesAsynchronously(forKeys: ["duration"], completionHandler: { () -> Void in - DispatchQueue.main.async { - completion(asset) - } - }) + DispatchQueue.global(qos: .background).async { + let asset = AVURLAsset(url: self.URL, options: nil) + asset.loadValuesAsynchronously(forKeys: ["duration"], completionHandler: { () -> Void in + DispatchQueue.main.async { + completion(asset) + } + }) + } } fileprivate func configureMetadata()