diff --git a/LeStorage/ApiCallCollection.swift b/LeStorage/ApiCallCollection.swift index 68f5d0d..49ddf17 100644 --- a/LeStorage/ApiCallCollection.swift +++ b/LeStorage/ApiCallCollection.swift @@ -34,6 +34,8 @@ actor ApiCallCollection: SomeCallCollection { /// Indicates if the collection is currently retrying ApiCalls fileprivate var _isRetryingCalls: Bool = false + fileprivate var _executionTask: Task? = nil + /// Indicates whether the collection content has changed /// Initiates a write when true fileprivate var _hasChanged: Bool = false { @@ -107,17 +109,20 @@ actor ApiCallCollection: SomeCallCollection { self._hasChanged = true } } - func findCallById(_ id: String) async -> (any SomeCall)? { - return self.findById(id) - } - + /// Returns the Api call associated with the provided id func findById(_ id: String) -> ApiCall? { return self.items.first(where: { $0.id == id }) } + /// Returns the Api call associated with the provided id + func findCallById(_ id: String) async -> (any SomeCall)? { + return self.findById(id) + } + /// Removes all objects in memory and deletes the JSON file func reset() { + self._executionTask?.cancel() self.items.removeAll() do { @@ -140,7 +145,7 @@ actor ApiCallCollection: SomeCallCollection { self._isRetryingCalls = true self._attemptLoops += 1 - Task { + self._executionTask = Task { let delay = pow(2, self._attemptLoops) let seconds = NSDecimalNumber(decimal: delay).intValue @@ -168,7 +173,6 @@ actor ApiCallCollection: SomeCallCollection { } } - } // MARK: - Synchronization @@ -273,7 +277,7 @@ actor ApiCallCollection: SomeCallCollection { default: break } - Logger.log("") +// Logger.log("") } /// Returns the content of the API call file as a String diff --git a/LeStorage/Services.swift b/LeStorage/Services.swift index df339a6..dfc8f3a 100644 --- a/LeStorage/Services.swift +++ b/LeStorage/Services.swift @@ -213,7 +213,7 @@ public class Services { // MARK: - Services /// Executes a GET request - public func get(identifier: StoreIdentifier?) async throws -> [T] { + public func get(identifier: StoreIdentifier? = nil) async throws -> [T] { let getRequest = try _getRequest(type: T.self, identifier: identifier) return try await self._runRequest(getRequest) } diff --git a/LeStorage/Store.swift b/LeStorage/Store.swift index 975a512..d570668 100644 --- a/LeStorage/Store.swift +++ b/LeStorage/Store.swift @@ -26,6 +26,11 @@ public struct StoreIdentifier { var value: String var parameterName: String + public init(value: String, parameterName: String) { + self.value = value + self.parameterName = parameterName + } + var urlComponent: String { return "?\(self.parameterName)=\(self.value)" } diff --git a/LeStorage/StoredCollection.swift b/LeStorage/StoredCollection.swift index bed6b54..a164383 100644 --- a/LeStorage/StoredCollection.swift +++ b/LeStorage/StoredCollection.swift @@ -190,7 +190,9 @@ public class StoredCollection: RandomAccessCollection, SomeCollecti } do { let items: [T] = try await self._store.getItems() - try self._addOrUpdate(contentOfs: items, shouldSync: false) + if items.count > 0 { + try self._addOrUpdate(contentOfs: items, shouldSync: false) + } self.hasLoadedFromServer = true DispatchQueue.main.async { NotificationCenter.default.post(name: NSNotification.Name.CollectionDidLoad, object: self)