|
|
|
|
@ -30,6 +30,9 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti |
|
|
|
|
/// If true, will synchronize the data with the provided server located at the Store's synchronizationApiURL |
|
|
|
|
let synchronized: Bool |
|
|
|
|
|
|
|
|
|
/// Doesn't write the collection in a file |
|
|
|
|
fileprivate var _inMemory: Bool = false |
|
|
|
|
|
|
|
|
|
/// The list of stored items |
|
|
|
|
@Published public fileprivate(set) var items: [T] = [] |
|
|
|
|
|
|
|
|
|
@ -48,7 +51,10 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti |
|
|
|
|
fileprivate var _hasChanged: Bool = false { |
|
|
|
|
didSet { |
|
|
|
|
if self._hasChanged == true { |
|
|
|
|
self._scheduleWrite() |
|
|
|
|
|
|
|
|
|
if !self._inMemory { |
|
|
|
|
self._scheduleWrite() |
|
|
|
|
} |
|
|
|
|
DispatchQueue.main.async { |
|
|
|
|
NotificationCenter.default.post(name: NSNotification.Name.CollectionDidChange, object: self) |
|
|
|
|
} |
|
|
|
|
@ -60,12 +66,13 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti |
|
|
|
|
/// Denotes a collection that loads and writes asynchronousIO |
|
|
|
|
fileprivate var asynchronousIO: Bool = true |
|
|
|
|
|
|
|
|
|
init(synchronized: Bool, store: Store, indexed: Bool = false, asynchronousIO: Bool = true, loadCompletion: ((StoredCollection<T>) -> ())? = nil) { |
|
|
|
|
init(synchronized: Bool, store: Store, indexed: Bool = false, asynchronousIO: Bool = true, inMemory: Bool = false, loadCompletion: ((StoredCollection<T>) -> ())? = nil) { |
|
|
|
|
self.synchronized = synchronized |
|
|
|
|
self.asynchronousIO = asynchronousIO |
|
|
|
|
if indexed { |
|
|
|
|
self._index = [:] |
|
|
|
|
} |
|
|
|
|
self._inMemory = inMemory |
|
|
|
|
self._store = store |
|
|
|
|
self.loadCompletion = loadCompletion |
|
|
|
|
|
|
|
|
|
@ -82,29 +89,35 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti |
|
|
|
|
|
|
|
|
|
/// Migrates if necessary and asynchronously decodes the json file |
|
|
|
|
fileprivate func _load() { |
|
|
|
|
|
|
|
|
|
do { |
|
|
|
|
let url = try FileUtils.pathForFileInDocumentDirectory(T.fileName()) |
|
|
|
|
if FileManager.default.fileExists(atPath: url.path()) { |
|
|
|
|
|
|
|
|
|
if self.asynchronousIO { |
|
|
|
|
Task(priority: .high) { |
|
|
|
|
try await Store.main.performMigrationIfNecessary(self) |
|
|
|
|
try self._decodeJSONFile() |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
try self._decodeJSONFile() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if self._inMemory { |
|
|
|
|
try self.loadDataFromServer() |
|
|
|
|
} else { |
|
|
|
|
try self._loadFromFile() |
|
|
|
|
} |
|
|
|
|
// else { |
|
|
|
|
// try? self.loadDataFromServer() |
|
|
|
|
// } |
|
|
|
|
} catch { |
|
|
|
|
Logger.log(error) |
|
|
|
|
Logger.error(error) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fileprivate func _loadFromFile() throws { |
|
|
|
|
let url = try FileUtils.pathForFileInDocumentDirectory(T.fileName()) |
|
|
|
|
if FileManager.default.fileExists(atPath: url.path()) { |
|
|
|
|
|
|
|
|
|
if self.asynchronousIO { |
|
|
|
|
Task(priority: .high) { |
|
|
|
|
try await Store.main.performMigrationIfNecessary(self) |
|
|
|
|
try self._decodeJSONFile() |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
try self._decodeJSONFile() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Decodes the json file into the items array |
|
|
|
|
fileprivate func _decodeJSONFile() throws { |
|
|
|
|
let jsonString = try FileUtils.readDocumentFile(fileName: T.fileName()) |
|
|
|
|
@ -355,22 +368,6 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Task { |
|
|
|
|
// do { |
|
|
|
|
// if let apiCall = try self._callForInstance(instance, method: Method.put) { |
|
|
|
|
// try self._prepareCall(apiCall: apiCall) |
|
|
|
|
// _ = try await self._store.execute(apiCall: apiCall) |
|
|
|
|
// } |
|
|
|
|
// |
|
|
|
|
//// let _ = try await self._store.service?.update(instance) |
|
|
|
|
// } catch { |
|
|
|
|
// Logger.error(error) |
|
|
|
|
// self.rescheduleApiCallsIfNecessary() |
|
|
|
|
// } |
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|