Skip to content

Commit

Permalink
Merge pull request onevcat#620 from gram30/fix-background-crash
Browse files Browse the repository at this point in the history
Use other method to enumerate contents, which causing crash(maybe only occurred for archved build)
  • Loading branch information
onevcat authored Mar 9, 2017
2 parents 5b6e4ae + 33b3f28 commit 4260203
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 4260203

Please sign in to comment.