|
|
|
|
@ -163,7 +163,7 @@ actor ApiCallCollection<T: SyncedStorable>: SomeCallCollection { |
|
|
|
|
|
|
|
|
|
/// Reschedule API calls if necessary |
|
|
|
|
func rescheduleApiCallsIfNecessary() { |
|
|
|
|
if self.items.isNotEmpty { |
|
|
|
|
if self.items.isNotEmpty && !self._isRescheduling { |
|
|
|
|
self._schedulingTask = Task { |
|
|
|
|
await self._rescheduleApiCalls() |
|
|
|
|
} |
|
|
|
|
@ -190,44 +190,16 @@ actor ApiCallCollection<T: SyncedStorable>: SomeCallCollection { |
|
|
|
|
if batch.count == 1, let apiCall = batch.first, apiCall.method == .get { |
|
|
|
|
let _: Empty = try await self._executeGetCall(apiCall) |
|
|
|
|
} else { |
|
|
|
|
let success = try await self._executeApiCalls(batch) |
|
|
|
|
let results = try await self._executeApiCalls(batch) |
|
|
|
|
if T.copyServerResponse { |
|
|
|
|
StoreCenter.main.updateLocalInstances(success) |
|
|
|
|
StoreCenter.main.updateLocalInstances(results) |
|
|
|
|
} |
|
|
|
|
self._attemptLoops = -1 |
|
|
|
|
} |
|
|
|
|
self._attemptLoops = -1 |
|
|
|
|
} catch { |
|
|
|
|
Logger.error(error) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// let apiCallsCopy = self.items |
|
|
|
|
// for apiCall in apiCallsCopy { |
|
|
|
|
// apiCall.attemptsCount += 1 |
|
|
|
|
// apiCall.lastAttemptDate = Date() |
|
|
|
|
// |
|
|
|
|
// do { |
|
|
|
|
// switch apiCall.method { |
|
|
|
|
// case .post: |
|
|
|
|
// let result: T = try await self._executeApiCall(apiCall) |
|
|
|
|
// StoreCenter.main.updateFromServerInstance(result) |
|
|
|
|
//// Logger.log("\(T.resourceName()) > SUCCESS!") |
|
|
|
|
// case .put: |
|
|
|
|
// let _: T = try await self._executeApiCall(apiCall) |
|
|
|
|
// case .delete: |
|
|
|
|
// let _: Empty = try await self._executeApiCall(apiCall) |
|
|
|
|
// case .get: |
|
|
|
|
// if T.self == GetSyncData.self { |
|
|
|
|
// let _: Empty = try await self._executeApiCall(apiCall) |
|
|
|
|
// } else { |
|
|
|
|
// let _: [T] = try await self._executeApiCall(apiCall) |
|
|
|
|
// } |
|
|
|
|
// } |
|
|
|
|
// } catch { |
|
|
|
|
//// Logger.log("\(T.resourceName()) > API CALL RETRY ERROR:") |
|
|
|
|
//// Logger.error(error) |
|
|
|
|
// } |
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
self._isRescheduling = false |
|
|
|
|
if self.items.isNotEmpty { |
|
|
|
|
@ -339,43 +311,6 @@ actor ApiCallCollection<T: SyncedStorable>: SomeCallCollection { |
|
|
|
|
return try await self._executeApiCalls(apiCalls) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Sends an insert api call for the provided [instance] |
|
|
|
|
// func sendInsertion(_ instance: T) async throws -> T? { |
|
|
|
|
// do { |
|
|
|
|
// return try await self._sendServerRequest(HTTPMethod.post, instance: instance) |
|
|
|
|
// } catch { |
|
|
|
|
// self.rescheduleApiCallsIfNecessary() |
|
|
|
|
// StoreCenter.main.log(message: "POST failed for \(instance): \(error.localizedDescription)") |
|
|
|
|
// Logger.error(error) |
|
|
|
|
// } |
|
|
|
|
// return nil |
|
|
|
|
// |
|
|
|
|
// } |
|
|
|
|
// |
|
|
|
|
// /// Sends an update api call for the provided [instance] |
|
|
|
|
// func sendUpdate(_ instance: T) async throws -> T? { |
|
|
|
|
// do { |
|
|
|
|
// return try await self._sendServerRequest(HTTPMethod.put, instance: instance) |
|
|
|
|
// } catch { |
|
|
|
|
// self.rescheduleApiCallsIfNecessary() |
|
|
|
|
// StoreCenter.main.log(message: "PUT failed for \(instance): \(error.localizedDescription)") |
|
|
|
|
// Logger.error(error) |
|
|
|
|
// } |
|
|
|
|
// return nil |
|
|
|
|
// } |
|
|
|
|
// |
|
|
|
|
// /// Sends an delete api call for the provided [instance] |
|
|
|
|
// func sendDeletion(_ instance: T) async throws { |
|
|
|
|
// do { |
|
|
|
|
// let _: Empty? = try await self._sendServerRequest(HTTPMethod.delete, instance: instance) |
|
|
|
|
// } catch { |
|
|
|
|
// self.rescheduleApiCallsIfNecessary() |
|
|
|
|
// StoreCenter.main.log(message: "DELETE failed for \(instance): \(error.localizedDescription)") |
|
|
|
|
// Logger.error(error) |
|
|
|
|
// } |
|
|
|
|
// return |
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
/// Initiates the process of sending the data with the server |
|
|
|
|
fileprivate func _sendServerRequest<V: Decodable>(_ method: HTTPMethod, instance: T? = nil) async throws -> V? { |
|
|
|
|
if let apiCall = try self._call(method: method, instance: instance) { |
|
|
|
|
|