From 9b1ce21f6f215d091dbd81a62e0827ab257d2ceb Mon Sep 17 00:00:00 2001 From: Satindar Dhillon Date: Mon, 12 Sep 2022 13:49:08 -0700 Subject: [PATCH] remove explicit try call for decoding SpeechDocument --- .../AudioSession/AudioController.swift | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/apple/OmnivoreKit/Sources/Services/AudioSession/AudioController.swift b/apple/OmnivoreKit/Sources/Services/AudioSession/AudioController.swift index 9c3ae765a..a145f1d9d 100644 --- a/apple/OmnivoreKit/Sources/Services/AudioSession/AudioController.swift +++ b/apple/OmnivoreKit/Sources/Services/AudioSession/AudioController.swift @@ -531,7 +531,7 @@ public class AudioController: NSObject, ObservableObject, AVAudioPlayerDelegate } } - func downloadSpeechFile(itemID: String, priority: DownloadPriority) async throws -> SpeechDocument { + func downloadSpeechFile(itemID: String, priority: DownloadPriority) async throws -> SpeechDocument? { let decoder = JSONDecoder() let speechFileUrl = pathForSpeechFile(itemID: itemID) @@ -569,14 +569,16 @@ public class AudioController: NSObject, ObservableObject, AVAudioPlayerDelegate let str = String(decoding: data, as: UTF8.self) print("result speech file: ", str) - let document = try! JSONDecoder().decode(SpeechDocument.self, from: data) + let document = try? JSONDecoder().decode(SpeechDocument.self, from: data) - // Cache the file - do { - try? FileManager.default.createDirectory(at: document.audioDirectory, withIntermediateDirectories: true) - try data.write(to: speechFileUrl) - } catch { - print("error writing file", error) + // Cache the file - if it exists + if let document = document { + do { + try? FileManager.default.createDirectory(at: document.audioDirectory, withIntermediateDirectories: true) + try data.write(to: speechFileUrl) + } catch { + print("error writing file", error) + } } return document