From 7a9f560ccf76217de6128e799af4ad9d6d01a762 Mon Sep 17 00:00:00 2001 From: Laurent Date: Tue, 9 Jul 2024 17:13:07 +0200 Subject: [PATCH] cleanup and documentation --- LeStorage/ApiCallCollection.swift | 9 +++++---- LeStorage/ModelObject.swift | 1 + LeStorage/Services.swift | 3 +-- LeStorage/Store.swift | 5 ----- LeStorage/StoredCollection.swift | 29 ++++++++--------------------- 5 files changed, 15 insertions(+), 32 deletions(-) diff --git a/LeStorage/ApiCallCollection.swift b/LeStorage/ApiCallCollection.swift index 72b8920..86784ad 100644 --- a/LeStorage/ApiCallCollection.swift +++ b/LeStorage/ApiCallCollection.swift @@ -34,7 +34,8 @@ actor ApiCallCollection: SomeCallCollection { /// Indicates if the collection is currently retrying ApiCalls fileprivate var _isRetryingCalls: Bool = false - fileprivate var _executionTask: Task? = nil + /// The task of waiting and executing ApiCalls + fileprivate var _reschedulingTask: Task? = nil /// Indicates whether the collection content has changed /// Initiates a write when true @@ -122,7 +123,7 @@ actor ApiCallCollection: SomeCallCollection { /// Removes all objects in memory and deletes the JSON file func reset() { - self._executionTask?.cancel() + self._reschedulingTask?.cancel() self.items.removeAll() do { @@ -145,11 +146,11 @@ actor ApiCallCollection: SomeCallCollection { self._isRetryingCalls = true self._attemptLoops += 1 - self._executionTask = Task { + self._reschedulingTask = Task { let delay = pow(2, self._attemptLoops) let seconds = NSDecimalNumber(decimal: delay).intValue - Logger.log("wait for \(seconds) sec") + Logger.log("\(T.resourceName()): wait for \(seconds) sec") try await Task.sleep(until: .now + .seconds(seconds)) let apiCallsCopy = self.items diff --git a/LeStorage/ModelObject.swift b/LeStorage/ModelObject.swift index 7958a52..a4baf46 100644 --- a/LeStorage/ModelObject.swift +++ b/LeStorage/ModelObject.swift @@ -8,6 +8,7 @@ import Foundation /// A class used as the root class for Storable objects +/// Provides default implementations of the Storable protocol open class ModelObject { public var store: Store? = nil diff --git a/LeStorage/Services.swift b/LeStorage/Services.swift index 148ce02..e59091d 100644 --- a/LeStorage/Services.swift +++ b/LeStorage/Services.swift @@ -240,6 +240,7 @@ public class Services { /// Executes an ApiCall func runApiCall(_ apiCall: ApiCall) async throws -> T { let request = try self._request(from: apiCall) + print("HTTP \(request.httpMethod ?? "") : id = \(apiCall.dataId)") return try await self._runRequest(request, apiCallId: apiCall.id) } @@ -298,9 +299,7 @@ public class Services { /// - password: the account's password public func requestToken(username: String, password: String) async throws -> String { var postRequest = try self._baseRequest(conf: .requestToken) - let deviceId = StoreCenter.main.deviceId() - Logger.log("deviceId = \(deviceId)") let credentials = Credentials(username: username, password: password, deviceId: deviceId) postRequest.httpBody = try jsonEncoder.encode(credentials) let response: AuthResponse = try await self._runRequest(postRequest) diff --git a/LeStorage/Store.swift b/LeStorage/Store.swift index e34361a..89d632b 100644 --- a/LeStorage/Store.swift +++ b/LeStorage/Store.swift @@ -8,11 +8,6 @@ import Foundation import UIKit -//public enum ResetOption { -// case all -// case synchronizedOnly -//} - public enum StoreError: Error { case missingService case missingUserId diff --git a/LeStorage/StoredCollection.swift b/LeStorage/StoredCollection.swift index c6b5fbd..1792333 100644 --- a/LeStorage/StoredCollection.swift +++ b/LeStorage/StoredCollection.swift @@ -158,18 +158,10 @@ public class StoredCollection: RandomAccessCollection, SomeCollecti } else { self._setAsLoaded() } -// else if self.synchronized { -// Task { -// do { -// try await self.loadDataFromServerIfAllowed() -// } catch { -// Logger.error(error) -// } -// } -// } - } + /// Sets the collection as loaded + /// Send a CollectionDidLoad event fileprivate func _setAsLoaded() { self.hasLoaded = true DispatchQueue.main.async { @@ -177,6 +169,7 @@ public class StoredCollection: RandomAccessCollection, SomeCollecti } } + /// Sets a collection of items and indexes them fileprivate func _setItems(_ items: [T]) { self.items = items self._updateIndexIfNecessary() @@ -205,6 +198,7 @@ public class StoredCollection: RandomAccessCollection, SomeCollecti self._setAsLoaded() } + /// Loads the collection using the server data only if the collection file doesn't exists func loadCollectionsFromServerIfNoFile() async throws { let fileURL: URL = try self._store.fileURL(type: T.self) if !FileManager.default.fileExists(atPath: fileURL.path()) { @@ -288,6 +282,7 @@ public class StoredCollection: RandomAccessCollection, SomeCollecti try self._addOrUpdate(contentOfs: sequence) } + /// Adds or update a sequence of elements without synchronizing it func addOrUpdateNoSync(contentOfs sequence: any Sequence) throws { try self._addOrUpdate(contentOfs: sequence, shouldSync: false) } @@ -360,12 +355,14 @@ public class StoredCollection: RandomAccessCollection, SomeCollecti // MARK: - Migrations + /// Makes POST ApiCall for all items in the collection public func insertAllIntoCurrentService() { for item in self.items { self._sendInsertionIfNecessary(item) } } + /// Makes POST ApiCall for the provided item public func insertIntoCurrentService(item: T) { self._sendInsertionIfNecessary(item) } @@ -410,20 +407,10 @@ public class StoredCollection: RandomAccessCollection, SomeCollecti self.items.removeAll() } - /// Removes the items of the collection, deletes the corresponding file, and also reset the related API calls collection + /// Removes the items of the collection and deletes the corresponding file public func reset() { self.items.removeAll() self._store.removeFile(type: T.self) -// do { -// let url: URL = try T.urlForJSONFile() -// if FileManager.default.fileExists(atPath: url.path()) { -// try FileManager.default.removeItem(at: url) -// } -// } catch { -// Logger.error(error) -// } - -// self.resetApiCalls() } // MARK: - Reschedule calls