diff --git a/LeStorage/Store.swift b/LeStorage/Store.swift index f0c8d6d..1832cf8 100644 --- a/LeStorage/Store.swift +++ b/LeStorage/Store.swift @@ -165,6 +165,7 @@ public class Store { /// Registers an api call into its collection func registerApiCall(_ apiCall: ApiCall) throws { + let collection: StoredCollection> = try self.apiCallCollection() if let existingDataCall = collection.first(where: { $0.dataId == apiCall.dataId }) { diff --git a/LeStorage/StoredCollection.swift b/LeStorage/StoredCollection.swift index 5db082e..500459d 100644 --- a/LeStorage/StoredCollection.swift +++ b/LeStorage/StoredCollection.swift @@ -23,11 +23,7 @@ public class StoredCollection : RandomAccessCollection, SomeCollec let synchronized: Bool /// The list of stored items - @Published public fileprivate(set) var items: [T] = [] { - didSet { - self._hasChanged = true - } - } + @Published public fileprivate(set) var items: [T] = [] /// The reference to the Store fileprivate var _store: Store @@ -109,7 +105,7 @@ public class StoredCollection : RandomAccessCollection, SomeCollec /// Updates the whole index with the items array fileprivate func _updateIndexIfNecessary() { - if let index = self._index { + if let _ = self._index { self._index = self.items.dictionary(handler: { $0.stringId }) } } @@ -122,6 +118,7 @@ public class StoredCollection : RandomAccessCollection, SomeCollec Task { do { self.items = try await self._store.getItems() + self._hasChanged = true } catch { Logger.error(error) } @@ -133,32 +130,38 @@ public class StoredCollection : RandomAccessCollection, SomeCollec /// 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) { - defer { - self._hasChanged = true - } - // update - if let index = self.items.firstIndex(where: { $0.id == instance.id }) { - self.items[index] = instance - self._sendUpdateIfNecessary(instance) - } else { // insert - self.items.append(instance) - self._index?[instance.stringId] = instance - self._sendInsertionIfNecessary(instance) - } +// DispatchQueue(label: "lestorage.queue.items").sync { + + defer { + self._hasChanged = true + } + + // update + if let index = self.items.firstIndex(where: { $0.id == instance.id }) { + self.items[index] = instance + self._sendUpdateIfNecessary(instance) + } else { // insert + self.items.append(instance) + self._index?[instance.stringId] = instance + self._sendInsertionIfNecessary(instance) + } +// } } /// Deletes the instance in the collection by id public func delete(instance: T) throws { - defer { - self._hasChanged = true - } + + defer { + self._hasChanged = true + } + + try instance.deleteDependencies() + self.items.removeAll { $0.id == instance.id } + self._index?.removeValue(forKey: instance.stringId) + self._sendDeletionIfNecessary(instance) - try instance.deleteDependencies() - self.items.removeAll { $0.id == instance.id } - self._index?.removeValue(forKey: instance.stringId) - self._sendDeletionIfNecessary(instance) } /// Inserts the whole sequence into the items array, no updates @@ -209,7 +212,7 @@ public class StoredCollection : RandomAccessCollection, SomeCollec /// Schedules a write operation fileprivate func _scheduleWrite() { if self.asynchronousIO { - DispatchQueue(label: "lestorage.queue.write", qos: .utility).async { + DispatchQueue(label: "lestorage.queue.write", qos: .utility).sync { // sync to make sure we don't have writes performed at the same time self._write() } } else { @@ -253,7 +256,7 @@ public class StoredCollection : RandomAccessCollection, SomeCollec Task { do { - let _ = try await self._store.service?.insert(instance) + let _ = try await self._store.service?.update(instance) } catch { Logger.error(error) }