From 33b3f28386a76740cc099d28e4bb5c51b2587324 Mon Sep 17 00:00:00 2001 From: Masayuki Ono Date: Thu, 9 Mar 2017 19:55:08 +0900 Subject: [PATCH] Use other method to enumerate contents, which causing crash(maybe only occurred for archved build) - Fix https://github.com/onevcat/Kingfisher/issues/619 --- Sources/ImageCache.swift | 58 +++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 31 deletions(-) diff --git a/Sources/ImageCache.swift b/Sources/ImageCache.swift index 19f92efb2..62aaa0b63 100755 --- a/Sources/ImageCache.swift +++ b/Sources/ImageCache.swift @@ -481,42 +481,38 @@ open class ImageCache { var cachedFiles = [URL: URLResourceValues]() var urlsToDelete = [URL]() var diskCacheSize: UInt = 0 - - if let fileEnumerator = self.fileManager.enumerator(at: diskCacheURL, includingPropertiesForKeys: Array(resourceKeys), options: FileManager.DirectoryEnumerationOptions.skipsHiddenFiles, errorHandler: nil), - let urls = fileEnumerator.allObjects as? [URL] - { - for fileUrl in urls { - - do { - let resourceValues = try fileUrl.resourceValues(forKeys: resourceKeys) - // If it is a Directory. Continue to next file URL. - if resourceValues.isDirectory == true { - continue - } - - // If this file is expired, add it to URLsToDelete - if !onlyForCacheSize, - let expiredDate = expiredDate, - let lastAccessData = resourceValues.contentAccessDate, - (lastAccessData as NSDate).laterDate(expiredDate) == expiredDate - { - urlsToDelete.append(fileUrl) - continue - } - if let fileSize = resourceValues.totalFileAllocatedSize { - diskCacheSize += UInt(fileSize) - if !onlyForCacheSize { - cachedFiles[fileUrl] = resourceValues - } + for fileUrl in (try? fileManager.contentsOfDirectory(at: diskCacheURL, includingPropertiesForKeys: Array(resourceKeys), options: .skipsHiddenFiles)) ?? [] { + + do { + let resourceValues = try fileUrl.resourceValues(forKeys: resourceKeys) + // If it is a Directory. Continue to next file URL. + if resourceValues.isDirectory == true { + continue + } + + // If this file is expired, add it to URLsToDelete + if !onlyForCacheSize, + let expiredDate = expiredDate, + let lastAccessData = resourceValues.contentAccessDate, + (lastAccessData as NSDate).laterDate(expiredDate) == expiredDate + { + urlsToDelete.append(fileUrl) + continue + } + + if let fileSize = resourceValues.totalFileAllocatedSize { + diskCacheSize += UInt(fileSize) + if !onlyForCacheSize { + cachedFiles[fileUrl] = resourceValues } - } catch _ { } - } + } + } catch _ { } } - + return (urlsToDelete, diskCacheSize, cachedFiles) } - + #if !os(macOS) && !os(watchOS) /** Clean expired disk cache when app in background. This is an async operation.