Cache
Cache copied to clipboard
Unable to cache object to Disk
trafficstars
When caching objects
func setupStorage() {
do {
let memoryConfig = MemoryConfig(expiry: .never)
let memoryStorage = MemoryStorage<String, [Lesson]>(config: memoryConfig)
let diskConfig = DiskConfig(name: CacheKey.diskConfig, directory: fileUrl())
let diskStorage = try DiskStorage<String, [Lesson]>(
config: diskConfig,
transformer: TransformerFactory.forCodable(ofType: [Lesson].self)
)
let hybridStorage = HybridStorage(memoryStorage: memoryStorage, diskStorage: diskStorage)
self.storage = Storage(hybridStorage: hybridStorage)
} catch {
self.error = AppError.cacheError(description: error.localizedDescription)
}
}
private func fetchLessons() async {
do {
let lessons = try await networkService.fetchLessons()
self.lessons = lessons
cache.insert(lessons, forKey: CacheKey.key)
try storage?.setObject(lessons, forKey: CacheKey.key, expiry: .never)
let entry = try storage?.entry(forKey: CacheKey.key)
print(entry?.filePath)
} catch {
self.error = AppError.errorFetchingLessons(description: error.localizedDescription)
}
}
entry?.filePath is always nil
let doesExist = storage!.objectExists(forKey: CacheKey.key) always return false when app is closed and open again
even when I don't manually add the directory to the DiskConfig this still happens