Просмотр исходного кода

Prevent heavy work in maybeCachedCheckingQueue

And also try to improve the iterator perform since Swift now has a better & modern API
onevcat 11 месяцев назад
Родитель
Сommit
e115556062
1 измененных файлов с 10 добавлено и 8 удалено
  1. 10 8
      Sources/Cache/DiskStorage.swift

+ 10 - 8
Sources/Cache/DiskStorage.swift

@@ -100,17 +100,19 @@ public enum DiskStorage {
         }
         }
 
 
         private func setupCacheChecking() {
         private func setupCacheChecking() {
-            maybeCachedCheckingQueue.async {
+            DispatchQueue.global(qos: .default).async {
                 do {
                 do {
-                    self.maybeCached = Set()
-                    try self.config.fileManager.contentsOfDirectory(atPath: self.directoryURL.path).forEach { 
-                        fileName in
-                        self.maybeCached?.insert(fileName)
+                    let allFiles = try self.config.fileManager.contentsOfDirectory(atPath: self.directoryURL.path)
+                    let maybeCached = Set(allFiles)
+                    self.maybeCachedCheckingQueue.async {
+                        self.maybeCached = maybeCached
                     }
                     }
                 } catch {
                 } catch {
-                    // Just disable the functionality if we fail to initialize it properly. This will just revert to
-                    // the behavior which is to check file existence on disk directly.
-                    self.maybeCached = nil
+                    self.maybeCachedCheckingQueue.async {
+                        // Just disable the functionality if we fail to initialize it properly. This will just revert to
+                        // the behavior which is to check file existence on disk directly.
+                        self.maybeCached = nil
+                    }
                 }
                 }
             }
             }
         }
         }