Fix issue with items being copied for no reason

sync2
Laurent 10 months ago
parent ae1aa39c5d
commit 64130849e8
  1. 9
      LeStorage/StoredCollection+Sync.swift
  2. 7
      LeStorage/StoredCollection.swift

@ -59,6 +59,11 @@ extension StoredCollection: SomeSyncedCollection where T : SyncedStorable {
/// 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) {
guard T.copyServerResponse else {
return
}
DispatchQueue.main.async {
if let localInstance = self.findById(serverInstance.id) {
localInstance.copy(from: serverInstance)
@ -104,7 +109,7 @@ extension StoredCollection: SomeSyncedCollection where T : SyncedStorable {
/// Adds or update an instance and writes
public func addOrUpdate(instance: T) {
Logger.log("\(T.resourceName()) : one item")
// Logger.log("\(T.resourceName()) : one item")
defer {
self.setChanged()
}
@ -121,7 +126,7 @@ extension StoredCollection: SomeSyncedCollection where T : SyncedStorable {
/// Adds or update a sequence and writes
public func addOrUpdate(contentOfs sequence: any Sequence<T>) {
Logger.log("\(T.resourceName()) : \(sequence.underestimatedCount) items")
// Logger.log("\(T.resourceName()) : \(sequence.underestimatedCount) items")
defer {
self.setChanged()
}

@ -257,7 +257,12 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti
/// Updates an instance to the collection by index
func updateItem(_ instance: T, index: Int) {
self.items[index].copy(from: instance)
let item = self.items[index]
if item !== instance {
self.items[index].copy(from: instance)
}
instance.store = self.store
self._indexes?[instance.id] = instance
}

Loading…
Cancel
Save