Slide 13
Slide 13 text
static func save(response: Response, fileName: String)
throws {
guard let fileUrl = FileCacheHandler.filePath(fileName: fileName,
pagination: response.header) else {
throw CacheError.cachesPathNotExist
}
do {
let data = try JSONEncoder().encode(response.body)
let directoryUrl = fileUrl.deletingLastPathComponent()
if !FileManager.default.fileExists(atPath: directoryUrl.path) {
try FileManager.default.createDirectory(at: directoryUrl,
withIntermediateDirectories: true, attributes: nil)
}
try data.write(to: fileUrl)
print("saved: \(fileUrl)")
} catch {
print(error.localizedDescription)
throw error
}
}