|
|
|
|
@ -250,21 +250,15 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti |
|
|
|
|
self.items.append(instance) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Deletes the instance in the collection by id |
|
|
|
|
/// Deletes the instance in the collection by id and sets the collection as changed to trigger a write |
|
|
|
|
public func delete(instance: T) throws { |
|
|
|
|
|
|
|
|
|
defer { |
|
|
|
|
self._hasChanged = true |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
try instance.deleteDependencies() |
|
|
|
|
self.items.removeAll { $0.id == instance.id } |
|
|
|
|
self._indexes?.removeValue(forKey: instance.id) |
|
|
|
|
|
|
|
|
|
self._sendDeletionIfNecessary(instance) |
|
|
|
|
try self._delete(instance) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Deletes all items of the sequence by id |
|
|
|
|
/// Deletes all items of the sequence by id and sets the collection as changed to trigger a write |
|
|
|
|
public func delete(contentOfs sequence: any Sequence<T>) throws { |
|
|
|
|
|
|
|
|
|
defer { |
|
|
|
|
@ -272,13 +266,22 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for instance in sequence { |
|
|
|
|
try instance.deleteDependencies() |
|
|
|
|
self.items.removeAll { $0.id == instance.id } |
|
|
|
|
self._indexes?.removeValue(forKey: instance.id) |
|
|
|
|
self._sendDeletionIfNecessary(instance) |
|
|
|
|
try self._delete(instance) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Deletes an instance in the collection. Also: |
|
|
|
|
/// - Removes its reference from the index |
|
|
|
|
/// - Notifies the server of the deletion |
|
|
|
|
/// - Calls `hasBeenDeleted` on the deleted instance |
|
|
|
|
fileprivate func _delete(_ instance: T) throws { |
|
|
|
|
try instance.deleteDependencies() |
|
|
|
|
self.items.removeAll { $0.id == instance.id } |
|
|
|
|
self._indexes?.removeValue(forKey: instance.id) |
|
|
|
|
self._sendDeletionIfNecessary(instance) |
|
|
|
|
instance.hasBeenDeleted() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Adds or update a sequence of elements |
|
|
|
|
public func addOrUpdate(contentOfs sequence: any Sequence<T>) throws { |
|
|
|
|
self._addOrUpdate(contentOfs: sequence) |
|
|
|
|
@ -339,6 +342,7 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti |
|
|
|
|
if let index = self.items.firstIndex(where: { $0.id == item.id }) { |
|
|
|
|
self.items.remove(at: index) |
|
|
|
|
} |
|
|
|
|
item.hasBeenDeleted() |
|
|
|
|
|
|
|
|
|
Task { |
|
|
|
|
do { |
|
|
|
|
@ -437,6 +441,9 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Updates a local item from a server instance. This method is typically used when the server makes update |
|
|
|
|
/// to an object when it's inserted. The StoredCollection possibly needs to update its own copy with new values. |
|
|
|
|
/// - serverInstance: the instance of the object on the server |
|
|
|
|
func updateFromServerInstance(_ serverInstance: T) { |
|
|
|
|
DispatchQueue.main.async { |
|
|
|
|
if let localInstance = self.findById(serverInstance.id) { |
|
|
|
|
|