Skip to content

Commit

Permalink
Use other method to enumerate contents, which causing crash(maybe onl…
Browse files Browse the repository at this point in the history
…y occurred for archved build)

- Fix onevcat#619
  • Loading branch information
mono0926 committed Mar 9, 2017
1 parent 5b6e4ae commit 33b3f28
Showing 1 changed file with 27 additions and 31 deletions.
58 changes: 27 additions & 31 deletions Sources/ImageCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 33b3f28

Please sign in to comment.