Created
July 11, 2019 11:48
-
-
Save chriseidhof/8ed01fc1169304603f9ac01e2fd50c7a to your computer and use it in GitHub Desktop.
Image Manager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final fileprivate class Weak<A: AnyObject> { | |
weak var value: A? | |
init(_ value: A) { | |
self.value = value | |
} | |
} | |
final class ImageManager { | |
fileprivate var values: [URL:Weak<Resource<UIImage>>] = [:] | |
func load(url: URL) -> Resource<UIImage> { | |
if let v = values[url]?.value { return v } | |
let loader = Resource(Endpoint(imageURL: url)) | |
values[url] = Weak(loader) | |
return loader | |
} | |
static let shared = ImageManager() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment