diff --git a/Haneke/Cache.swift b/Haneke/Cache.swift index 99ce0735..2a56452d 100644 --- a/Haneke/Cache.swift +++ b/Haneke/Cache.swift @@ -150,7 +150,7 @@ open class Cache where T.Result == T, T : DataRepresentable Log.error(message: "Failed to remove path \(path)", error: error) } if let completion = completion { - DispatchQueue.main.async { + HanekeGlobals.Queue.async { completion() } } @@ -231,7 +231,7 @@ open class Cache where T.Result == T, T : DataRepresentable let value = T.convertFromData(data) if let value = value { let descompressedValue = self.decompressedImageIfNeeded(value) - DispatchQueue.main.async(execute: { + HanekeGlobals.Queue.async(execute: { succeed(descompressedValue) let wrapper = ObjectWrapper(value: descompressedValue) memoryCache.setObject(wrapper, forKey: key as AnyObject) @@ -264,7 +264,7 @@ open class Cache where T.Result == T, T : DataRepresentable } } - DispatchQueue.main.async { + HanekeGlobals.Queue.async { succeed(formatted) } } diff --git a/Haneke/DiskCache.swift b/Haneke/DiskCache.swift index e7cf0608..ae287f02 100644 --- a/Haneke/DiskCache.swift +++ b/Haneke/DiskCache.swift @@ -60,13 +60,13 @@ open class DiskCache { let path = self.path(forKey: key) do { let data = try Data(contentsOf: URL(fileURLWithPath: path), options: Data.ReadingOptions()) - DispatchQueue.main.async { + HanekeGlobals.Queue.async { succeed(data) } self.updateDiskAccessDate(atPath: path) } catch { if let block = fail { - DispatchQueue.main.async { + HanekeGlobals.Queue.async { block(error) } } @@ -100,7 +100,7 @@ open class DiskCache { Log.error(message: "Failed to list directory", error: error) } if let completion = completion { - DispatchQueue.main.async { + HanekeGlobals.Queue.async { completion() } } diff --git a/Haneke/DiskFetcher.swift b/Haneke/DiskFetcher.swift index a034f0e1..f99ae85a 100644 --- a/Haneke/DiskFetcher.swift +++ b/Haneke/DiskFetcher.swift @@ -59,7 +59,7 @@ open class DiskFetcher : Fetcher { do { data = try Data(contentsOf: URL(fileURLWithPath: self.path), options: Data.ReadingOptions()) } catch { - DispatchQueue.main.async { + HanekeGlobals.Queue.async { if self.cancelled { return } @@ -76,13 +76,13 @@ open class DiskFetcher : Fetcher { let localizedFormat = NSLocalizedString("Failed to convert value from data at path %@", comment: "Error description") let description = String(format:localizedFormat, self.path) let error = errorWithCode(HanekeGlobals.DiskFetcher.ErrorCode.invalidData.rawValue, description: description) - DispatchQueue.main.async { + HanekeGlobals.Queue.async { fail(error) } return } - DispatchQueue.main.async(execute: { + HanekeGlobals.Queue.async(execute: { if self.cancelled { return } diff --git a/Haneke/Haneke.swift b/Haneke/Haneke.swift index c5b63f7c..89515b2b 100644 --- a/Haneke/Haneke.swift +++ b/Haneke/Haneke.swift @@ -11,6 +11,9 @@ import UIKit public struct HanekeGlobals { public static let Domain = "io.haneke" + public static let Queue:DispatchQueue = { + return DispatchQueue(label: "io.haneke.callback_queue") + }() } diff --git a/Haneke/NetworkFetcher.swift b/Haneke/NetworkFetcher.swift index 2f50c842..04d5b616 100644 --- a/Haneke/NetworkFetcher.swift +++ b/Haneke/NetworkFetcher.swift @@ -69,7 +69,7 @@ open class NetworkFetcher : Fetcher { if ((error as NSError).domain == NSURLErrorDomain && (error as NSError).code == NSURLErrorCancelled) { return } Log.debug(message: "Request \(URL.absoluteString) failed", error: error) - DispatchQueue.main.async(execute: { fail(error) }) + HanekeGlobals.Queue.async(execute: { fail(error) }) return } @@ -93,13 +93,13 @@ open class NetworkFetcher : Fetcher { return } - DispatchQueue.main.async { succeed(value) } + HanekeGlobals.Queue.async { succeed(value) } } fileprivate func failWithCode(_ code: HanekeGlobals.NetworkFetcher.ErrorCode, localizedDescription: String, failure fail: @escaping ((Error?) -> ())) { let error = errorWithCode(code.rawValue, description: localizedDescription) Log.debug(message: localizedDescription, error: error) - DispatchQueue.main.async { fail(error) } + HanekeGlobals.Queue.async { fail(error) } } } diff --git a/Haneke/UIImageView+Haneke.swift b/Haneke/UIImageView+Haneke.swift index 99a2162d..f3364223 100644 --- a/Haneke/UIImageView+Haneke.swift +++ b/Haneke/UIImageView+Haneke.swift @@ -47,7 +47,9 @@ public extension UIImageView { if didSetImage { return } if let placeholder = placeholder { - self.image = placeholder + DispatchQueue.main.async { + self.image = placeholder + } } } @@ -121,9 +123,11 @@ public extension UIImageView { if let succeed = succeed { succeed(image) } else if animated { - UIView.transition(with: self, duration: HanekeGlobals.UIKit.SetImageAnimationDuration, options: .transitionCrossDissolve, animations: { - self.image = image - }, completion: nil) + DispatchQueue.main.async { + UIView.transition(with: self, duration: HanekeGlobals.UIKit.SetImageAnimationDuration, options: .transitionCrossDissolve, animations: { + self.image = image + }, completion: nil) + } } else { self.image = image }