|
|
|
|
@ -150,12 +150,14 @@ public class BaseCollection<T: Storable>: SomeCollection, CollectionHolder { |
|
|
|
|
guard let manager = self._pendingOperationManager, manager.items.isNotEmpty else { return } |
|
|
|
|
|
|
|
|
|
Logger.log(">>> Merge pending: \(manager.items.count)") |
|
|
|
|
for operation in manager.items { |
|
|
|
|
switch operation.method { |
|
|
|
|
let methodGroups = manager.items.group { $0.method } |
|
|
|
|
for (method, group) in methodGroups { |
|
|
|
|
let dataArray = group.map { $0.data } |
|
|
|
|
switch method { |
|
|
|
|
case .addOrUpdate: |
|
|
|
|
self.addOrUpdate(instance: operation.data) |
|
|
|
|
self.addOrUpdate(contentOfs: dataArray) |
|
|
|
|
case .delete: |
|
|
|
|
self.delete(instance: operation.data) |
|
|
|
|
self.delete(contentOfs: dataArray) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
self._pendingOperationManager = nil |
|
|
|
|
@ -243,16 +245,16 @@ public class BaseCollection<T: Storable>: SomeCollection, CollectionHolder { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Adds a sequence of objects inside the collection and performs a write |
|
|
|
|
func addSequence(_ sequence: any Sequence<T>) { |
|
|
|
|
func addSequence(_ sequence: any Sequence<T>, checkLoaded: Bool = true) { |
|
|
|
|
defer { |
|
|
|
|
self._hasChanged = true |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for instance in sequence { |
|
|
|
|
if let index = self.items.firstIndex(where: { $0.id == instance.id }) { |
|
|
|
|
self.updateItem(instance, index: index) |
|
|
|
|
self.updateItem(instance, index: index, checkLoaded: checkLoaded) |
|
|
|
|
} else { // insert |
|
|
|
|
self.addItem(instance: instance) |
|
|
|
|
self.addItem(instance: instance, checkLoaded: checkLoaded) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -270,9 +272,9 @@ public class BaseCollection<T: Storable>: SomeCollection, CollectionHolder { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Adds an instance to the collection |
|
|
|
|
@discardableResult func addItem(instance: T) -> Bool { |
|
|
|
|
@discardableResult func addItem(instance: T, checkLoaded: Bool = true) -> Bool { |
|
|
|
|
|
|
|
|
|
if !self.hasLoaded { |
|
|
|
|
if checkLoaded && !self.hasLoaded { |
|
|
|
|
self._addPendingOperation(method: .addOrUpdate, instance: instance) |
|
|
|
|
return false |
|
|
|
|
} |
|
|
|
|
@ -286,9 +288,9 @@ public class BaseCollection<T: Storable>: SomeCollection, CollectionHolder { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Updates an instance to the collection by index |
|
|
|
|
@discardableResult func updateItem(_ instance: T, index: Int) -> Bool { |
|
|
|
|
@discardableResult func updateItem(_ instance: T, index: Int, checkLoaded: Bool = true) -> Bool { |
|
|
|
|
|
|
|
|
|
if !self.hasLoaded { |
|
|
|
|
if checkLoaded && !self.hasLoaded { |
|
|
|
|
self._addPendingOperation(method: .addOrUpdate, instance: instance) |
|
|
|
|
return false |
|
|
|
|
} |
|
|
|
|
|