let savePath = FileManager.documentsDirectory.appendingPathComponent("myData.data") init() { do { let data = try Data(contentsOf: savePath) let historyData = try JSONDecoder().decode([SingleSample].self, from: data) DispatchQueue.main.async{ self.history = historyData } } catch { history = [] } } func save(history: [SingleSample]) { do { let data = try JSONEncoder().encode(history) try data.write(to: savePath, options: [.atomic, .completeFileProtection]) } catch { print("Unable to save data.") } } func load() async throws { do { let data = try Data(contentsOf: savePath) let historyData = try JSONDecoder().decode([SingleSample].self, from: data) DispatchQueue.main.async { self.history = historyData } } catch { history = [] } }