adds method to clear before loading

sync2
Laurent 11 months ago
parent a03ee52c70
commit bbf7e8fa8b
  1. 9
      LeStorage/StoredCollection.swift

@ -183,8 +183,12 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti
}
}
/// Retrieves the data from the server and loads it into the items array
public func loadDataFromServerIfAllowed() async throws {
try await self.loadDataFromServerIfAllowed(clear: false)
}
/// Retrieves the data from the server and loads it into the items array
public func loadDataFromServerIfAllowed(clear: Bool = false) async throws {
guard self.synchronized, !(self is StoredSingleton<T>) else {
throw StoreError.cannotSyncCollection(name: self.resourceName)
}
@ -192,6 +196,9 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti
let items: [T] = try await self._store.getItems()
if items.count > 0 {
DispatchQueue.main.async {
if clear {
self.clear()
}
self._addOrUpdate(contentOfs: items, shouldSync: false)
}
}

Loading…
Cancel
Save