From c5168f1ace7bc359fca807013d09c34eaa3bcf71 Mon Sep 17 00:00:00 2001 From: Laurent Date: Sat, 22 Mar 2025 15:19:34 +0100 Subject: [PATCH] Improve rescheduling --- LeStorage/ApiCallCollection.swift | 3 +- LeStorage/StoredCollection+Sync.swift | 62 ++++++++++++++------------- 2 files changed, 34 insertions(+), 31 deletions(-) diff --git a/LeStorage/ApiCallCollection.swift b/LeStorage/ApiCallCollection.swift index 1589243..cc74281 100644 --- a/LeStorage/ApiCallCollection.swift +++ b/LeStorage/ApiCallCollection.swift @@ -164,6 +164,7 @@ actor ApiCallCollection: SomeCallCollection { } } + /// Reschedule API calls without waiting func rescheduleImmediately() { self._attemptLoops = -1 self.rescheduleApiCallsIfNecessary() @@ -333,7 +334,7 @@ actor ApiCallCollection: SomeCallCollection { let call = try self.callForInstance(delete, method: .delete, transactionId: transactionId) apiCalls.append(call) } - self.rescheduleApiCallsIfNecessary() + self.rescheduleImmediately() // return try await self._executeApiCalls(apiCalls) } diff --git a/LeStorage/StoredCollection+Sync.swift b/LeStorage/StoredCollection+Sync.swift index dd267c8..a827b69 100644 --- a/LeStorage/StoredCollection+Sync.swift +++ b/LeStorage/StoredCollection+Sync.swift @@ -80,37 +80,8 @@ extension StoredCollection: SomeSyncedCollection where T : SyncedStorable { } } - // MARK: - Basic operations + // MARK: - Basic operations with sync - /// Adds or update an instance without synchronizing it - func addOrUpdateNoSync(_ instance: T) throws { - self.addOrUpdateItem(instance: instance) - } - - /// Adds or update a sequence of elements without synchronizing it - func addOrUpdateNoSync(contentOfs sequence: any Sequence) { - self.addSequence(sequence) - } - - /// Deletes the instance in the collection without synchronization - func deleteNoSync(instance: T) throws { - defer { - self.setChanged() - } - self.deleteItem(instance) - } - - /// Deletes the instance in the collection without synchronization - func deleteByStringIdNoSync(_ id: String) { - defer { - self.setChanged() - } - let realId = T.buildRealId(id: id) - if let instance = self.findById(realId) { - self.deleteItem(instance) - } - } - /// Adds or update an instance and writes public func addOrUpdate(instance: T) { // Logger.log("\(T.resourceName()) : one item") @@ -201,6 +172,37 @@ extension StoredCollection: SomeSyncedCollection where T : SyncedStorable { delete(contentOfs: items) // MUST NOT ADD "self" before delete, otherwise it will call the delete method of StoredCollection without sync } + // MARK: - Basic operations without sync + + /// Adds or update an instance without synchronizing it + func addOrUpdateNoSync(_ instance: T) throws { + self.addOrUpdateItem(instance: instance) + } + + /// Adds or update a sequence of elements without synchronizing it + func addOrUpdateNoSync(contentOfs sequence: any Sequence) { + self.addSequence(sequence) + } + + /// Deletes the instance in the collection without synchronization + func deleteNoSync(instance: T) throws { + defer { + self.setChanged() + } + self.deleteItem(instance) + } + + /// Deletes the instance in the collection without synchronization + func deleteByStringIdNoSync(_ id: String) { + defer { + self.setChanged() + } + let realId = T.buildRealId(id: id) + if let instance = self.findById(realId) { + self.deleteItem(instance) + } + } + // MARK: - Send requests fileprivate func _sendInsertion(_ instance: T) {