|
|
|
|
@ -89,6 +89,24 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti |
|
|
|
|
self._load() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// MARK: - Paths |
|
|
|
|
|
|
|
|
|
fileprivate func _storageDirectoryPath() throws -> URL { |
|
|
|
|
return try FileUtils.pathForDirectoryInDocuments(directory: Store.storageDirectory) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fileprivate func _writeToStorageDirectory(content: String, fileName: String) throws { |
|
|
|
|
var fileURL = try self._storageDirectoryPath() |
|
|
|
|
fileURL.append(component: fileName) |
|
|
|
|
try content.write(to: fileURL, atomically: false, encoding: .utf8) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fileprivate func _pathForFile(_ fileName: String) throws -> URL { |
|
|
|
|
var storageDirectory = try self._storageDirectoryPath() |
|
|
|
|
storageDirectory.append(component: fileName) |
|
|
|
|
return storageDirectory |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// MARK: - Loading |
|
|
|
|
|
|
|
|
|
/// Migrates if necessary and asynchronously decodes the json file |
|
|
|
|
@ -107,7 +125,7 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fileprivate func _loadFromFile() throws { |
|
|
|
|
let url: URL = try FileUtils.pathForFileInDocumentDirectory(T.fileName()) |
|
|
|
|
let url: URL = try self._pathForFile(T.fileName()) |
|
|
|
|
if FileManager.default.fileExists(atPath: url.path()) { |
|
|
|
|
|
|
|
|
|
if self.asynchronousIO { |
|
|
|
|
@ -124,7 +142,8 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti |
|
|
|
|
|
|
|
|
|
/// Decodes the json file into the items array |
|
|
|
|
fileprivate func _decodeJSONFile() throws { |
|
|
|
|
let jsonString: String = try FileUtils.readDocumentFile(fileName: T.fileName()) |
|
|
|
|
let fileURL = try self._pathForFile(T.fileName()) |
|
|
|
|
let jsonString: String = try FileUtils.readFile(fileURL: fileURL) |
|
|
|
|
if let decoded: [T] = try jsonString.decodeArray() { |
|
|
|
|
DispatchQueue.main.async { |
|
|
|
|
Logger.log("loaded \(T.fileName()) with \(decoded.count) items") |
|
|
|
|
@ -279,7 +298,8 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti |
|
|
|
|
Logger.log("Start write...") |
|
|
|
|
do { |
|
|
|
|
let jsonString: String = try self.items.jsonString() |
|
|
|
|
let _ = try FileUtils.writeToDocumentDirectory(content: jsonString, fileName: T.fileName()) |
|
|
|
|
try self._writeToStorageDirectory(content: jsonString, fileName: T.fileName()) |
|
|
|
|
// let _ = try FileUtils.writeToDocumentDirectory(content: jsonString, fileName: T.fileName()) |
|
|
|
|
} catch { |
|
|
|
|
Logger.error(error) // TODO how to notify the main project |
|
|
|
|
} |
|
|
|
|
@ -476,8 +496,4 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// public func append(_ newElement: T) { |
|
|
|
|
// self.addOrUpdate(instance: newElement) |
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|