Is there a way to check if a remote .lottie file is cached when using DotLottie for iOS? #2562
Unanswered
saha-sagnik
asked this question in
Q&A
Replies: 1 comment
-
Lottie doesn't have built-in cache status checking for remote files, but you can implement this with a simple caching
layer:
```swift
final class LottieCache {
static let shared = LottieCache()
private let cacheDirectory: URL = {
let paths = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)
return paths[0].appendingPathComponent("LottieCache")
}()
private init() {
try? FileManager.default.createDirectory(at: cacheDirectory, withIntermediateDirectories: true)
}
// Check if animation is cached
func isCached(url: URL) -> Bool {
let localPath = localURL(for: url)
return FileManager.default.fileExists(atPath: localPath.path)
}
// Get cached animation or download
func animation(from url: URL) async throws -> LottieAnimation? {
let localPath = localURL(for: url)
// Return cached if exists
if FileManager.default.fileExists(atPath: localPath.path) {
return LottieAnimation.filepath(localPath.path)
}
// Download and cache
let (data, _) = try await URLSession.shared.data(from: url)
try data.write(to: localPath)
return LottieAnimation.filepath(localPath.path)
}
private func localURL(for remoteURL: URL) -> URL {
let filename = remoteURL.absoluteString.data(using: .utf8)!.base64EncodedString()
return cacheDirectory.appendingPathComponent(filename + ".lottie")
}
// Clear cache if needed
func clearCache() {
try? FileManager.default.removeItem(at: cacheDirectory)
try? FileManager.default.createDirectory(at: cacheDirectory, withIntermediateDirectories: true)
}
}
// Usage
if LottieCache.shared.isCached(url: animationURL) {
print("Animation is cached!")
}
let animation = try await LottieCache.shared.animation(from: animationURL)
For .dotlottie files specifically, you can adapt this to use DotLottieFile instead of LottieAnimation. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi everyone,
I’m currently using the DotLottie iOS SDK to load and play .lottie files from remote URLs in a SwiftUI app.
I want to know if there’s a built-in method or property provided by the DotLottie SDK to check whether a .lottie file is already cached (either internally or on disk), or if it was freshly downloaded.
My use case is to:
• Load a .lottie file from a remote URL
• Determine whether it’s coming from cache or the network
• Optionally notify the UI via a callback
I noticed that methods like DotLottieFile.loadedFrom(url:completion:) don’t indicate the cache status in their result. Is caching handled internally at all by the SDK? Or is manual disk caching via FileManager still the only reliable way to do this?
Any insights or suggestions would be really appreciated!
Thanks in advance.
Beta Was this translation helpful? Give feedback.
All reactions