|
|
|
|
@ -23,7 +23,11 @@ public class StoredCollection<T : Storable> : RandomAccessCollection, SomeCollec |
|
|
|
|
let synchronized: Bool |
|
|
|
|
|
|
|
|
|
/// The list of stored items |
|
|
|
|
@Published public fileprivate(set) var items: [T] = [] |
|
|
|
|
@Published public fileprivate(set) var items: [T] = [] { |
|
|
|
|
didSet { |
|
|
|
|
self._hasChanged = true |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// The reference to the Store |
|
|
|
|
fileprivate var _store: Store |
|
|
|
|
@ -61,6 +65,8 @@ public class StoredCollection<T : Storable> : RandomAccessCollection, SomeCollec |
|
|
|
|
let url = try FileUtils.directoryURLForFileName(self._fileName) |
|
|
|
|
if FileManager.default.fileExists(atPath: url.path()) { |
|
|
|
|
self._loadAsync() |
|
|
|
|
} else { |
|
|
|
|
try? self.loadDataFromServer() |
|
|
|
|
} |
|
|
|
|
} catch { |
|
|
|
|
Logger.log(error) |
|
|
|
|
@ -89,6 +95,19 @@ public class StoredCollection<T : Storable> : RandomAccessCollection, SomeCollec |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public func loadDataFromServer() throws { |
|
|
|
|
guard self.synchronized else { |
|
|
|
|
throw StoreError.unSynchronizedCollection |
|
|
|
|
} |
|
|
|
|
Task { |
|
|
|
|
do { |
|
|
|
|
self.items = try await self._store.getItems() |
|
|
|
|
} catch { |
|
|
|
|
Logger.error(error) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Adds or updates the provided instance inside the collection |
|
|
|
|
/// Adds it if its id is not found, and otherwise updates it |
|
|
|
|
public func addOrUpdate(instance: T) { |
|
|
|
|
|