multistore
Laurent 2 years ago
parent 239a47e17b
commit 2a5a6e6c48
  1. 1
      LeStorage/Store.swift
  2. 19
      LeStorage/StoredCollection.swift

@ -165,6 +165,7 @@ public class Store {
/// Registers an api call into its collection /// Registers an api call into its collection
func registerApiCall<T : Storable>(_ apiCall: ApiCall<T>) throws { func registerApiCall<T : Storable>(_ apiCall: ApiCall<T>) throws {
let collection: StoredCollection<ApiCall<T>> = try self.apiCallCollection() let collection: StoredCollection<ApiCall<T>> = try self.apiCallCollection()
if let existingDataCall = collection.first(where: { $0.dataId == apiCall.dataId }) { if let existingDataCall = collection.first(where: { $0.dataId == apiCall.dataId }) {

@ -23,11 +23,7 @@ public class StoredCollection<T : Storable> : RandomAccessCollection, SomeCollec
let synchronized: Bool let synchronized: Bool
/// The list of stored items /// 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 /// The reference to the Store
fileprivate var _store: Store fileprivate var _store: Store
@ -109,7 +105,7 @@ public class StoredCollection<T : Storable> : RandomAccessCollection, SomeCollec
/// Updates the whole index with the items array /// Updates the whole index with the items array
fileprivate func _updateIndexIfNecessary() { fileprivate func _updateIndexIfNecessary() {
if let index = self._index { if let _ = self._index {
self._index = self.items.dictionary(handler: { $0.stringId }) self._index = self.items.dictionary(handler: { $0.stringId })
} }
} }
@ -122,6 +118,7 @@ public class StoredCollection<T : Storable> : RandomAccessCollection, SomeCollec
Task { Task {
do { do {
self.items = try await self._store.getItems() self.items = try await self._store.getItems()
self._hasChanged = true
} catch { } catch {
Logger.error(error) Logger.error(error)
} }
@ -133,6 +130,9 @@ public class StoredCollection<T : Storable> : RandomAccessCollection, SomeCollec
/// Adds or updates the provided instance inside the collection /// Adds or updates the provided instance inside the collection
/// Adds it if its id is not found, and otherwise updates it /// Adds it if its id is not found, and otherwise updates it
public func addOrUpdate(instance: T) { public func addOrUpdate(instance: T) {
// DispatchQueue(label: "lestorage.queue.items").sync {
defer { defer {
self._hasChanged = true self._hasChanged = true
} }
@ -146,11 +146,13 @@ public class StoredCollection<T : Storable> : RandomAccessCollection, SomeCollec
self._index?[instance.stringId] = instance self._index?[instance.stringId] = instance
self._sendInsertionIfNecessary(instance) self._sendInsertionIfNecessary(instance)
} }
// }
} }
/// Deletes the instance in the collection by id /// Deletes the instance in the collection by id
public func delete(instance: T) throws { public func delete(instance: T) throws {
defer { defer {
self._hasChanged = true self._hasChanged = true
} }
@ -159,6 +161,7 @@ public class StoredCollection<T : Storable> : RandomAccessCollection, SomeCollec
self.items.removeAll { $0.id == instance.id } self.items.removeAll { $0.id == instance.id }
self._index?.removeValue(forKey: instance.stringId) self._index?.removeValue(forKey: instance.stringId)
self._sendDeletionIfNecessary(instance) self._sendDeletionIfNecessary(instance)
} }
/// Inserts the whole sequence into the items array, no updates /// Inserts the whole sequence into the items array, no updates
@ -209,7 +212,7 @@ public class StoredCollection<T : Storable> : RandomAccessCollection, SomeCollec
/// Schedules a write operation /// Schedules a write operation
fileprivate func _scheduleWrite() { fileprivate func _scheduleWrite() {
if self.asynchronousIO { 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() self._write()
} }
} else { } else {
@ -253,7 +256,7 @@ public class StoredCollection<T : Storable> : RandomAccessCollection, SomeCollec
Task { Task {
do { do {
let _ = try await self._store.service?.insert(instance) let _ = try await self._store.service?.update(instance)
} catch { } catch {
Logger.error(error) Logger.error(error)
} }

Loading…
Cancel
Save