From 63692c572bb92465288a3213089c7b50926a0880 Mon Sep 17 00:00:00 2001 From: Laurent Date: Wed, 3 Jul 2024 09:58:00 +0200 Subject: [PATCH] Fixes regression with data copy after inserts --- LeStorage/ApiCallCollection.swift | 67 +++++++++++++------------------ LeStorage/Services.swift | 4 -- LeStorage/Store.swift | 12 +++--- LeStorage/StoreCenter.swift | 18 ++++----- LeStorage/StoredCollection.swift | 23 +++++++++-- 5 files changed, 62 insertions(+), 62 deletions(-) diff --git a/LeStorage/ApiCallCollection.swift b/LeStorage/ApiCallCollection.swift index 3c826e7..14355b7 100644 --- a/LeStorage/ApiCallCollection.swift +++ b/LeStorage/ApiCallCollection.swift @@ -220,64 +220,53 @@ actor ApiCallCollection: SomeCallCollection { } /// Sends an insert api call for the provided [instance] - func sendInsertion(_ instance: T) { - Task { - do { - try await self._synchronize(instance, method: HTTPMethod.post) - } catch { - self.rescheduleApiCallsIfNecessary() - Logger.error(error) - } + func sendInsertion(_ instance: T) async throws -> T? { + do { + return try await self._synchronize(instance, method: HTTPMethod.post) + } catch { + self.rescheduleApiCallsIfNecessary() + Logger.error(error) } + return nil + } /// Sends an update api call for the provided [instance] - func sendUpdate(_ instance: T) { - Task { - do { - try await self._synchronize(instance, method: HTTPMethod.put) - } catch { - self.rescheduleApiCallsIfNecessary() - Logger.error(error) - } + func sendUpdate(_ instance: T) async throws -> T? { + do { + return try await self._synchronize(instance, method: HTTPMethod.put) + } catch { + self.rescheduleApiCallsIfNecessary() + Logger.error(error) } - + return nil } /// Sends an delete api call for the provided [instance] - func sendDeletion(_ instance: T) { - Task { - do { - try await self._synchronize(instance, method: HTTPMethod.delete) - } catch { - self.rescheduleApiCallsIfNecessary() - Logger.error(error) - } + func sendDeletion(_ instance: T) async throws -> T? { + do { + return try await self._synchronize(instance, method: HTTPMethod.delete) + } catch { + self.rescheduleApiCallsIfNecessary() + Logger.error(error) } - + return nil } /// Initiates the process of sending the data with the server - fileprivate func _synchronize(_ instance: T, method: HTTPMethod) async throws { + fileprivate func _synchronize(_ instance: T, method: HTTPMethod) async throws -> T? { if let apiCall = try self._callForInstance(instance, method: method) { try self._prepareCall(apiCall: apiCall) - try await self._executeApiCall(apiCall) + return try await self._executeApiCall(apiCall) + } else { + return nil } } /// Executes an API call /// For POST requests, potentially copies additional data coming from the server during the insert - fileprivate func _executeApiCall(_ apiCall: ApiCall) async throws { - let result = try await StoreCenter.main.execute(apiCall: apiCall) - switch apiCall.method { - case .post: - if let instance = self.findById(result.stringId) { - self._hasChanged = instance.copyFromServerInstance(result) - } - default: - break - } -// Logger.log("") + fileprivate func _executeApiCall(_ apiCall: ApiCall) async throws -> T { + return try await StoreCenter.main.execute(apiCall: apiCall) } /// Returns the content of the API call file as a String diff --git a/LeStorage/Services.swift b/LeStorage/Services.swift index dfc8f3a..4b29dd7 100644 --- a/LeStorage/Services.swift +++ b/LeStorage/Services.swift @@ -109,10 +109,6 @@ public class Services { try await StoreCenter.main.deleteApiCallById(apiCallId, collectionName: collectionName) } default: - /* - request ended with status code = 401 - {"detail":"Informations d'authentification non fournies."} - */ Logger.log("Failed Run \(request.httpMethod ?? "") \(request.url?.absoluteString ?? "")") let errorString: String = String(data: task.0, encoding: .utf8) ?? "" var errorMessage = ErrorMessage(error: errorString, domain: "") diff --git a/LeStorage/Store.swift b/LeStorage/Store.swift index 6eadb7b..33aa576 100644 --- a/LeStorage/Store.swift +++ b/LeStorage/Store.swift @@ -220,22 +220,22 @@ open class Store { /// Requests an insertion to the StoreCenter /// - Parameters: /// - instance: an object to insert - func sendInsertion(_ instance: T) async throws { - try await StoreCenter.main.sendInsertion(instance) + func sendInsertion(_ instance: T) async throws -> T? { + return try await StoreCenter.main.sendInsertion(instance) } /// Requests an update to the StoreCenter /// - Parameters: /// - instance: an object to update - func sendUpdate(_ instance: T) async throws { - try await StoreCenter.main.sendUpdate(instance) + @discardableResult func sendUpdate(_ instance: T) async throws -> T? { + return try await StoreCenter.main.sendUpdate(instance) } /// Requests a deletion to the StoreCenter /// - Parameters: /// - instance: an object to delete - func sendDeletion(_ instance: T) async throws { - try await StoreCenter.main.sendDeletion(instance) + @discardableResult func sendDeletion(_ instance: T) async throws -> T? { + return try await StoreCenter.main.sendDeletion(instance) } public func loadCollectionsFromServerIfNoFile() { diff --git a/LeStorage/StoreCenter.swift b/LeStorage/StoreCenter.swift index e70fede..c0c188e 100644 --- a/LeStorage/StoreCenter.swift +++ b/LeStorage/StoreCenter.swift @@ -355,31 +355,31 @@ public class StoreCenter { /// Transmit the insertion request to the ApiCall collection /// - Parameters: /// - instance: an object to insert - func sendInsertion(_ instance: T) async throws { + func sendInsertion(_ instance: T) async throws -> T? { guard self._canSynchronise() else { - return + return nil } - try await self.apiCallCollection().sendInsertion(instance) + return try await self.apiCallCollection().sendInsertion(instance) } /// Transmit the update request to the ApiCall collection /// - Parameters: /// - instance: an object to update - func sendUpdate(_ instance: T) async throws { + func sendUpdate(_ instance: T) async throws -> T? { guard self._canSynchronise() else { - return + return nil } - try await self.apiCallCollection().sendUpdate(instance) + return try await self.apiCallCollection().sendUpdate(instance) } /// Transmit the deletion request to the ApiCall collection /// - Parameters: /// - instance: an object to delete - func sendDeletion(_ instance: T) async throws { + func sendDeletion(_ instance: T) async throws -> T? { guard self._canSynchronise() else { - return + return nil } - try await self.apiCallCollection().sendDeletion(instance) + return try await self.apiCallCollection().sendDeletion(instance) } } diff --git a/LeStorage/StoredCollection.swift b/LeStorage/StoredCollection.swift index 0fad05b..9690432 100644 --- a/LeStorage/StoredCollection.swift +++ b/LeStorage/StoredCollection.swift @@ -414,7 +414,8 @@ public class StoredCollection: RandomAccessCollection, SomeCollecti // MARK: - Reschedule calls - /// Sends an insert api call for the provided [instance] + /// Sends an insert api call for the provided + /// Calls copyFromServerInstance on the instance with the result of the HTTP call /// - Parameters: /// - instance: the object to POST fileprivate func _sendInsertionIfNecessary(_ instance: T) { @@ -422,7 +423,13 @@ public class StoredCollection: RandomAccessCollection, SomeCollecti return } Task { - try await self._store.sendInsertion(instance) + do { + if let result = try await self._store.sendInsertion(instance) { + self._hasChanged = instance.copyFromServerInstance(result) + } + } catch { + Logger.error(error) + } } } @@ -434,7 +441,11 @@ public class StoredCollection: RandomAccessCollection, SomeCollecti return } Task { - try await self._store.sendUpdate(instance) + do { + try await self._store.sendUpdate(instance) + } catch { + Logger.error(error) + } } } @@ -446,7 +457,11 @@ public class StoredCollection: RandomAccessCollection, SomeCollecti return } Task { - try await self._store.sendDeletion(instance) + do { + try await self._store.sendDeletion(instance) + } catch { + Logger.error(error) + } } }