Created
May 19, 2021 09:33
-
-
Save Polenoso/7d2419e071da4a1a8ee306e1f0882f2a to your computer and use it in GitHub Desktop.
Lottie Cache Medium Post
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
//////////// AnimationPublic.swift | |
static func loadedFrom(url: URL, | |
closure: @escaping Animation.DownloadClosure, | |
animationCache: AnimationCacheProvider?) { | |
/// 1 | |
if let animationCache = animationCache, let animation = animationCache.animation(forKey: url.absoluteString) { | |
closure(animation) | |
} else { | |
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in | |
guard error == nil, let jsonData = data else { | |
DispatchQueue.main.async { | |
closure(nil) | |
} | |
return | |
} | |
do { | |
let animation = try JSONDecoder().decode(Animation.self, from: jsonData) | |
DispatchQueue.main.async { | |
/// 2 | |
animationCache?.setAnimation(animation, forKey: url.absoluteString) | |
closure(animation) | |
} | |
} catch { | |
DispatchQueue.main.async { | |
closure(nil) | |
} | |
} | |
} | |
task.resume() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment