From 75ee4f18e94b70dc12cf691ddee93fe46c6307d9 Mon Sep 17 00:00:00 2001 From: Leko Murphy Date: Sat, 18 Feb 2023 18:42:54 -0600 Subject: [PATCH] fetch assets off main thread --- Source/Pages/Gallery/YPLibraryVC.swift | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/Source/Pages/Gallery/YPLibraryVC.swift b/Source/Pages/Gallery/YPLibraryVC.swift index 53b5451f1..898c95e87 100644 --- a/Source/Pages/Gallery/YPLibraryVC.swift +++ b/Source/Pages/Gallery/YPLibraryVC.swift @@ -228,15 +228,25 @@ internal final class YPLibraryVC: UIViewController, YPPermissionCheckable { } } - func refreshMediaRequest() { - let options = buildPHFetchOptions() + private func refreshMediaRequest() { + + // calls to fetchAssets can take a while for large libraries and we don't want to block the main thread + DispatchQueue.global().async { + let options = self.buildPHFetchOptions() - if let collection = mediaManager.collection { - mediaManager.fetchResult = PHAsset.fetchAssets(in: collection, options: options) - } else { - mediaManager.fetchResult = PHAsset.fetchAssets(with: options) + if let collection = self.mediaManager.collection { + self.mediaManager.fetchResult = PHAsset.fetchAssets(in: collection, options: options) + } else { + self.mediaManager.fetchResult = PHAsset.fetchAssets(with: options) + } + + DispatchQueue.main.async { + self.refreshMediaRequestAfterFetch() + } } - + } + + private func refreshMediaRequestAfterFetch() { if mediaManager.hasResultItems, let firstAsset = mediaManager.getAsset(at: 0) { changeAsset(firstAsset)