From 9531f12e4f5a6f3177a9fc3d8a09745278ccf31c Mon Sep 17 00:00:00 2001 From: Vegard Bieker Matthey Date: Tue, 4 Aug 2026 03:58:05 +0200 Subject: [PATCH] perf: load comics async --- ImageViewer/ViewController.swift | 53 ++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/ImageViewer/ViewController.swift b/ImageViewer/ViewController.swift index b3dcd0e..b5be9a3 100644 --- a/ImageViewer/ViewController.swift +++ b/ImageViewer/ViewController.swift @@ -249,27 +249,27 @@ final class ViewController: UIViewController, UIGestureRecognizerDelegate { preferredStyle: .alert ) alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil)) - do { - var directories: [URL] = [] + var directories: [URL] = [] - guard let contents = try? fileManager.contentsOfDirectory( - at: documentsURL, - includingPropertiesForKeys: [.isDirectoryKey], - options: [.skipsHiddenFiles] - ) - else { - alert.message = String(format: "Failed to read contents of directory: \(documentsURL)") - pendingAlert = alert - return - } + guard let contents = try? fileManager.contentsOfDirectory( + at: documentsURL, + includingPropertiesForKeys: [.isDirectoryKey], + options: [.skipsHiddenFiles] + ) + else { + alert.message = String(format: "Failed to read contents of directory: \(documentsURL)") + pendingAlert = alert + return + } - directories = contents.filter { url in - var isDirectory: ObjCBool = false - return fileManager.fileExists(atPath: url.path, isDirectory: &isDirectory) - && isDirectory.boolValue - }.sorted { $0.path < $1.path } + directories = contents.filter { url in + var isDirectory: ObjCBool = false + return fileManager.fileExists(atPath: url.path, isDirectory: &isDirectory) + && isDirectory.boolValue + }.sorted { $0.path < $1.path } - for dir in directories { + for dir in directories { + queue.async { [self] in if !fileManager.fileExists(atPath: dir.appendingPathComponent(metadataFilename).path) { getMetadataFromFilename(path: dir) if pendingAlert != nil { @@ -298,7 +298,15 @@ final class ViewController: UIViewController, UIGestureRecognizerDelegate { loadLocalState() let volCovP = ProgressIndices(v: state[currentPath.lastPathComponent]!.progress.v, c: 0, i: 0) let archivePath = getArchiveURL(volCovP.v) - let archive = try Archive(url: archivePath, accessMode: .read) + let archive: Archive! + do { + let a = try Archive(url: archivePath, accessMode: .read) + archive = a + } catch { + alert.message = String(format: "Could not open Archive: \(archivePath)") + pendingAlert = alert + return + } let filename = metadata.volumes[volCovP.v].chapters[volCovP.c].images[volCovP.i].filename! guard let targetEntry = archive[filename] else { alert.message = String(format: "Archive entry does not exist: \(filename)") @@ -327,11 +335,10 @@ final class ViewController: UIViewController, UIGestureRecognizerDelegate { path: dir ) ) + DispatchQueue.main.async { + self.comicCollectionView.reloadData() + } } - } catch { - pendingAlert = alert - alert.message = String(format: "Generic failure.") - print("Failed to read directories") } } -- 2.54.0