diff --git a/LeStorage/ApiCallCollection.swift b/LeStorage/ApiCallCollection.swift index ab3e6d7..8a3b6de 100644 --- a/LeStorage/ApiCallCollection.swift +++ b/LeStorage/ApiCallCollection.swift @@ -62,7 +62,6 @@ actor ApiCallCollection: SomeCallCollection { /// Reschedule Api calls if not empty func loadFromFile() throws { try self._decodeJSONFile() -// self.rescheduleApiCallsIfNecessary() } /// Returns the file URL of the collection @@ -155,15 +154,19 @@ actor ApiCallCollection: SomeCallCollection { } func resumeApiCalls() { - self._schedulingTask?.cancel() - self._attemptLoops = -1 - self.rescheduleApiCallsIfNecessary() + if self._schedulingTask != nil && self._attemptLoops > 2 { + self._schedulingTask?.cancel() + self._attemptLoops = -1 + self.rescheduleApiCallsIfNecessary() + } } /// Reschedule API calls if necessary func rescheduleApiCallsIfNecessary() { - self._schedulingTask = Task { - await self._rescheduleApiCalls() + if self.items.isNotEmpty { + self._schedulingTask = Task { + await self._rescheduleApiCalls() + } } } @@ -285,7 +288,7 @@ actor ApiCallCollection: SomeCallCollection { } /// Sends an insert api call for the provided [instance] - func sendGetIfNecessary(instance: T) async throws where T : URLParameterConvertible { + func sendGetRequest(instance: T) async throws where T : URLParameterConvertible { do { let apiCall = ApiCall(method: .get) apiCall.urlParameters = instance.queryParameters() diff --git a/LeStorage/StoreCenter.swift b/LeStorage/StoreCenter.swift index 0974b22..f7ed6e6 100644 --- a/LeStorage/StoreCenter.swift +++ b/LeStorage/StoreCenter.swift @@ -404,11 +404,15 @@ public class StoreCenter { func synchronizeLastUpdates() async throws { if let lastSync = self._settingsStorage.item.lastSynchronization { - let getSyncData = GetSyncData() - getSyncData.lastUpdate = lastSync - let sync: ApiCallCollection = try self.apiCallCollection() - try await sync.sendGetIfNecessary(instance: getSyncData) + let syncGetCollection: ApiCallCollection = try self.apiCallCollection() + if await syncGetCollection.hasPendingCalls() { + await syncGetCollection.rescheduleApiCallsIfNecessary() + } else { + let getSyncData = GetSyncData() + getSyncData.lastUpdate = lastSync + try await syncGetCollection.sendGetRequest(instance: getSyncData) + } } else { Logger.w("Can't sync due to missing saved date") } diff --git a/LeStorage/WebSocketManager.swift b/LeStorage/WebSocketManager.swift index 98d3ecc..bf83026 100644 --- a/LeStorage/WebSocketManager.swift +++ b/LeStorage/WebSocketManager.swift @@ -54,6 +54,7 @@ class WebSocketManager: ObservableObject { self.changeStatus(error.localizedDescription) print("Error in receiving message: \(error)") + self._setupWebSocket() case .success(let message): switch message { @@ -99,9 +100,10 @@ class WebSocketManager: ObservableObject { private func ping() { webSocketTask?.sendPing { error in - if let error = error { - print("Error in sending ping: \(error)") - self.changeStatus("ping failed: \(error.localizedDescription)") + self.changeStatus("ping failed: \(error?.localizedDescription ?? "")") + + if let error: NSError = error as NSError?, + error.domain == NSPOSIXErrorDomain && error.code == 57 { self._setupWebSocket() } }