From bbf7e8fa8b88585909045b6aae56d2146cf20d98 Mon Sep 17 00:00:00 2001 From: Laurent Date: Mon, 23 Dec 2024 22:24:32 +0100 Subject: [PATCH] adds method to clear before loading --- LeStorage/StoredCollection.swift | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/LeStorage/StoredCollection.swift b/LeStorage/StoredCollection.swift index aefa7a5..79d840c 100644 --- a/LeStorage/StoredCollection.swift +++ b/LeStorage/StoredCollection.swift @@ -183,8 +183,12 @@ public class StoredCollection: 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) else { throw StoreError.cannotSyncCollection(name: self.resourceName) } @@ -192,6 +196,9 @@ public class StoredCollection: 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) } }