From a277ac80fe4863954734b77dc64d99a0a8161472 Mon Sep 17 00:00:00 2001 From: Laurent Date: Fri, 16 Feb 2024 16:28:14 +0100 Subject: [PATCH] test file presence to avoid error --- LeStorage/MicroStorage.swift | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/LeStorage/MicroStorage.swift b/LeStorage/MicroStorage.swift index f40a8a9..c3a40f6 100644 --- a/LeStorage/MicroStorage.swift +++ b/LeStorage/MicroStorage.swift @@ -19,13 +19,17 @@ class MicroStorage { init() { var instance: T? = nil do { - let jsonString = try FileUtils.readDocumentFile(fileName: T.fileName) - if let decoded: T = try jsonString.decode() { - instance = decoded + let url = try FileUtils.directoryURLForFileName(T.fileName) + if FileManager.default.fileExists(atPath: url.absoluteString) { + let jsonString = try FileUtils.readDocumentFile(fileName: T.fileName) + if let decoded: T = try jsonString.decode() { + instance = decoded + } } } catch { Logger.error(error) } + self.item = instance ?? T() }