From 3cf90172d47d5882d9f8434782497df6d99f9a83 Mon Sep 17 00:00:00 2001 From: Laurent Date: Fri, 21 Feb 2025 15:10:06 +0100 Subject: [PATCH 1/3] Reduce log collection size from 1000 to 50 --- LeStorage/StoreCenter.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LeStorage/StoreCenter.swift b/LeStorage/StoreCenter.swift index 96bc011..7d74438 100644 --- a/LeStorage/StoreCenter.swift +++ b/LeStorage/StoreCenter.swift @@ -465,7 +465,7 @@ public class StoreCenter { if let logs = self._logs { return logs } else { - let logsCollection: StoredCollection = Store.main.registerCollection(synchronized: false, limit: 1000) + let logsCollection: StoredCollection = Store.main.registerCollection(synchronized: false, limit: 50) self._logs = logsCollection return logsCollection } From 541aa5e297834fd214de7b1c33a4402d7c51caa8 Mon Sep 17 00:00:00 2001 From: Raz Date: Mon, 3 Mar 2025 17:43:41 +0100 Subject: [PATCH 2/3] fix loadDataFromServerIfAllowed --- LeStorage/StoredCollection.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LeStorage/StoredCollection.swift b/LeStorage/StoredCollection.swift index 79d840c..dab0f5a 100644 --- a/LeStorage/StoredCollection.swift +++ b/LeStorage/StoredCollection.swift @@ -202,10 +202,10 @@ public class StoredCollection: RandomAccessCollection, SomeCollecti self._addOrUpdate(contentOfs: items, shouldSync: false) } } + self._setAsLoaded() } catch { Logger.error(error) } - self._setAsLoaded() } /// Loads the collection using the server data only if the collection file doesn't exists From 1ed82862130149f260d784a96587cef65ea2ea01 Mon Sep 17 00:00:00 2001 From: Laurent Date: Tue, 4 Mar 2025 16:17:17 +0100 Subject: [PATCH 3/3] Fix api call provisioning --- LeStorage/ApiCallCollection.swift | 43 +++++++++++-------------------- LeStorageTests/ApiCallTests.swift | 7 ++++- 2 files changed, 21 insertions(+), 29 deletions(-) diff --git a/LeStorage/ApiCallCollection.swift b/LeStorage/ApiCallCollection.swift index a917aea..46518c4 100644 --- a/LeStorage/ApiCallCollection.swift +++ b/LeStorage/ApiCallCollection.swift @@ -215,39 +215,26 @@ actor ApiCallCollection: SomeCallCollection { // MARK: - Synchronization /// Returns an APICall instance for the Storable [instance] and an HTTP [method] - /// The method updates existing calls or creates a new one + /// The method makes some clean up when necessary: + /// - When deleting, we delete other calls as they are unecessary + /// - When updating, we delete other PUT as we don't want them to be executed in random orders func callForInstance(_ instance: T, method: HTTPMethod) throws -> ApiCall? { - // cleanup - let existingCalls = self.items.filter { $0.dataId == instance.stringId } - if existingCalls.count > 1 { - StoreCenter.main.log(message: "There are multiple calls registered for a single item: \(T.resourceName()), id = \(instance.stringId)") - } - let currentHTTPMethod = existingCalls.first?.method - var call: ApiCall? = nil - if let currentHTTPMethod { - switch (currentHTTPMethod, method) { - case (.post, .put): - call = try self._createCall(instance, method: .post) - case (.post, .delete): - call = try self._createCall(instance, method: .delete) - case (.put, .put): - call = try self._createCall(instance, method: .put) - case (.put, .delete): - call = try self._createCall(instance, method: .delete) - default: - call = try self._createCall(instance, method: method) - StoreCenter.main.log(message: "case \(currentHTTPMethod) / \(method) should not happen") - } - } else { - call = try self._createCall(instance, method: method) - } - - if let call { + // cleanup if necessary + switch method { + case .delete: // we don't want anything else than a DELETE in the queue + let existingCalls = self.items.filter { $0.dataId == instance.stringId } self._deleteCalls(existingCalls) - self._prepareCall(apiCall: call) + case .put: // we don't want mixed PUT calls so we delete the others + let existingPuts = self.items.filter { $0.dataId == instance.stringId && $0.method == .put } + self._deleteCalls(existingPuts) + default: + break } + let call: ApiCall = try self._createCall(instance, method: method) + self._prepareCall(apiCall: call) + return call } diff --git a/LeStorageTests/ApiCallTests.swift b/LeStorageTests/ApiCallTests.swift index bd5a7eb..b2c594f 100644 --- a/LeStorageTests/ApiCallTests.swift +++ b/LeStorageTests/ApiCallTests.swift @@ -37,10 +37,13 @@ struct ApiCallTests { thing.name = "woo" let _ = try await collection.sendUpdate(thing) - await #expect(collection.items.count == 1) + await #expect(collection.items.count == 2) // one post and one put if let apiCall = await collection.items.first { #expect(apiCall.method == .post) } + if let apiCall = await collection.items.last { + #expect(apiCall.method == .put) + } let _ = try await collection.sendDeletion(thing) await #expect(collection.items.count == 1) @@ -60,6 +63,8 @@ struct ApiCallTests { thing.name = "woo" let _ = try await collection.sendUpdate(thing) + let _ = try await collection.sendUpdate(thing) + let _ = try await collection.sendUpdate(thing) await #expect(collection.items.count == 1) if let apiCall = await collection.items.first { #expect(apiCall.method == .put)