Skip to content

Instantly share code, notes, and snippets.

@Polenoso
Created May 19, 2021 09:33
Show Gist options
  • Save Polenoso/7d2419e071da4a1a8ee306e1f0882f2a to your computer and use it in GitHub Desktop.
Save Polenoso/7d2419e071da4a1a8ee306e1f0882f2a to your computer and use it in GitHub Desktop.
Lottie Cache Medium Post
//////////// 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