Adds indexed option when registering a collection + mass addOrUpdate

multistore
Laurent 2 years ago
parent f3aa070736
commit 2323c46273
  1. 4
      LeStorage/Store.swift
  2. 16
      LeStorage/StoredCollection.swift

@ -64,10 +64,10 @@ public class Store {
/// Registers a collection
/// [synchronize] denotes a collection which modification will be sent to the django server
public func registerCollection<T : Storable>(synchronized: Bool) -> StoredCollection<T> {
public func registerCollection<T : Storable>(synchronized: Bool, indexed: Bool = false) -> StoredCollection<T> {
// register collection
let collection = StoredCollection<T>(synchronized: synchronized, store: Store.main, loadCompletion: nil)
let collection = StoredCollection<T>(synchronized: synchronized, store: Store.main, indexed: indexed, loadCompletion: nil)
self._collections[T.resourceName()] = collection
// if synchronized { // register additional collection for api calls

@ -179,14 +179,22 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti
}
/// Inserts the whole sequence into the items array, no updates
public func append(contentOfs sequence: any Sequence<T>) throws {
public func addOrUpdate(contentOfs sequence: any Sequence<T>) throws {
defer {
self._hasChanged = true
}
self.items.append(contentsOf: sequence)
for instance in sequence {
try self._sendInsertionIfNecessary(instance)
if let index = self.items.firstIndex(where: { $0.id == instance.id }) {
self.items[index] = instance
try self._sendUpdateIfNecessary(instance)
} else { // insert
self.items.append(instance)
self._index?[instance.stringId] = instance
try self._sendInsertionIfNecessary(instance)
}
}
}
/// Returns the instance corresponding to the provided [id]
@ -226,7 +234,7 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti
/// Schedules a write operation
fileprivate func _scheduleWrite() {
if self.asynchronousIO {
DispatchQueue(label: "lestorage.queue.write", qos: .utility).sync { // sync to make sure we don't have writes performed at the same time
DispatchQueue(label: "lestorage.queue.write", qos: .utility).asyncAndWait { // sync to make sure we don't have writes performed at the same time
self._write()
}
} else {

Loading…
Cancel
Save