Fixes regression with data copy after inserts

sync
Laurent 1 year ago
parent db0602dcf7
commit 63692c572b
  1. 67
      LeStorage/ApiCallCollection.swift
  2. 4
      LeStorage/Services.swift
  3. 12
      LeStorage/Store.swift
  4. 18
      LeStorage/StoreCenter.swift
  5. 23
      LeStorage/StoredCollection.swift

@ -220,64 +220,53 @@ actor ApiCallCollection<T: Storable>: 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<T>) 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<T>) async throws -> T {
return try await StoreCenter.main.execute(apiCall: apiCall)
}
/// Returns the content of the API call file as a String

@ -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: "")

@ -220,22 +220,22 @@ open class Store {
/// Requests an insertion to the StoreCenter
/// - Parameters:
/// - instance: an object to insert
func sendInsertion<T: Storable>(_ instance: T) async throws {
try await StoreCenter.main.sendInsertion(instance)
func sendInsertion<T: Storable>(_ 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<T: Storable>(_ instance: T) async throws {
try await StoreCenter.main.sendUpdate(instance)
@discardableResult func sendUpdate<T: Storable>(_ 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<T: Storable>(_ instance: T) async throws {
try await StoreCenter.main.sendDeletion(instance)
@discardableResult func sendDeletion<T: Storable>(_ instance: T) async throws -> T? {
return try await StoreCenter.main.sendDeletion(instance)
}
public func loadCollectionsFromServerIfNoFile() {

@ -355,31 +355,31 @@ public class StoreCenter {
/// Transmit the insertion request to the ApiCall collection
/// - Parameters:
/// - instance: an object to insert
func sendInsertion<T: Storable>(_ instance: T) async throws {
func sendInsertion<T: Storable>(_ 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<T: Storable>(_ instance: T) async throws {
func sendUpdate<T: Storable>(_ 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<T: Storable>(_ instance: T) async throws {
func sendDeletion<T: Storable>(_ 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)
}
}

@ -414,7 +414,8 @@ public class StoredCollection<T: Storable>: 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<T: Storable>: 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<T: Storable>: 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<T: Storable>: RandomAccessCollection, SomeCollecti
return
}
Task {
try await self._store.sendDeletion(instance)
do {
try await self._store.sendDeletion(instance)
} catch {
Logger.error(error)
}
}
}

Loading…
Cancel
Save